daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class weather<getWristData> extends AppCompatActivity {
  2.     private static String loggedUser;
  3.     public boolean AutoSend=false;
  4. Button click;
  5. private TextView fetchDataView;
  6. private static final String BASE_URI="https://berthawristbandrestprovider.azurewebsites.net/api/wristbanddata";
  7. public Data measurment;
  8.  
  9.  
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_weather);
  14.         Intent navigate=getIntent();
  15.         loggedUser=navigate.getStringExtra(MainActivity.USERNAME);
  16.         measurment=new Data();
  17.  
  18.         click=(Button) findViewById(R.id.fetchData);
  19.         fetchDataView=(TextView) findViewById(R.id.text_1);
  20.  
  21.         click.setOnClickListener(new View.OnClickListener() {
  22.             @Override
  23.             public void onClick(View v) {
  24.  
  25.             }
  26.         });
  27.     }
  28.  
  29.  
  30. public static JSONObject JsonFromData(Data measurment, String loggedUser){
  31.         try {
  32.             JSONObject jsonObjectSend= new JSONObject();
  33.  
  34.             jsonObjectSend.put("deviceId",measurment.deviceId);
  35.             jsonObjectSend.put("pm25",measurment.pm25);
  36.             jsonObjectSend.put("pm10",measurment.pm10);
  37.             jsonObjectSend.put("co2",measurment.co2);
  38.             jsonObjectSend.put("03",measurment.o3);
  39.             jsonObjectSend.put("pressure",measurment.pressure);
  40.             jsonObjectSend.put("temperature",measurment.temperature);
  41.             jsonObjectSend.put("humidity",measurment.humidity);
  42.             jsonObjectSend.put("utc",measurment.utc);
  43.             jsonObjectSend.put("latitude",measurment.latitude);
  44.             jsonObjectSend.put("longtitude",measurment.longtitude);
  45.             jsonObjectSend.put("userId",loggedUser);
  46.             return jsonObjectSend;
  47.  
  48.         }
  49.  
  50.         catch (JSONException ex){
  51.  
  52.         }
  53.  
  54.         return new JSONObject();
  55. }
  56.     @Override
  57.     protected void onStart() {
  58.         super.onStart();
  59.         JSONObject jsonToSend = JsonFromData(measurment,loggedUser);
  60.         PostDataTask task = new PostDataTask();
  61.         task.execute("https://berthawristbandrestprovider.azurewebsites.net/api/wristbanddata",jsonToSend.toString());
  62.     }
  63.     public void addData(View view) {
  64.  
  65.         JSONObject jsonToSend = JsonFromData(measurment,loggedUser);
  66.         PostDataTask task = new PostDataTask();
  67.         Log.d("LOG",jsonToSend.toString());
  68.         task.execute("https://berthabackendrestprovider.azurewebsites.net/api/data", jsonToSend.toString());
  69.  
  70.  
  71.     }
  72.  
  73.     private class PostDataTask extends AsyncTask<String, Void, CharSequence> {
  74.  
  75.         @Override
  76.         protected CharSequence doInBackground(String... params) {
  77.             String urlString = params[0];
  78.             String jsonDocument = params[1];
  79.             try {
  80.                 URL url = new URL(urlString);
  81.                 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  82.                 connection.setRequestMethod("POST");
  83.                 connection.setDoOutput(true);
  84.                 connection.setRequestProperty("Content-Type", "application/json");
  85.                 connection.setRequestProperty("Accept", "application/json");
  86.                 OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
  87.                 osw.write(jsonDocument);
  88.                 osw.flush();
  89.                 osw.close();
  90.                 int responseCode = connection.getResponseCode();
  91.                 if (responseCode / 100 != 2) {
  92.                     String responseMessage = connection.getResponseMessage();
  93.                     throw new IOException("HTTP response code: " + responseCode + " " + responseMessage);
  94.                 }
  95.                 BufferedReader reader = new BufferedReader(
  96.                         new InputStreamReader(connection.getInputStream()));
  97.                 String line = reader.readLine();
  98.                 return line;
  99.             } catch (MalformedURLException ex) {
  100.                 cancel(true);
  101.                 String message = ex.getMessage() + " " + urlString;
  102.                 Log.e("BOOK", message);
  103.                 return message;
  104.             } catch (IOException ex) {
  105.                 cancel(true);
  106.                 Log.e("BOOK", ex.getMessage());
  107.                 return ex.getMessage();
  108.             }
  109.         }
  110.  
  111.         @Override
  112.         protected void onPostExecute(CharSequence charSequence) {
  113.             super.onPostExecute(charSequence);
  114.             TextView messageView = findViewById(R.id.text_1);
  115.             messageView.setText(charSequence);
  116.             AutoSend=true;
  117.             Log.d("MINE", charSequence.toString());
  118.             finish();
  119.         }
  120.  
  121.         @Override
  122.         protected void onCancelled(CharSequence charSequence) {
  123.             super.onCancelled(charSequence);
  124.             TextView messageView = findViewById(R.id.text_1);
  125.             messageView.setText(charSequence);
  126.             Log.d("MINE", charSequence.toString());
  127.             finish();
  128.         }
  129.     }
  130.  
  131.  
  132.     protected void back2(View view) {
  133.         finish();
  134.  
  135.     }
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top