Guest User

Untitled

a guest
Oct 11th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. public class DataTabelFragment extends Fragment {
  2.  
  3. public TextView sensor1;
  4.  
  5. jsonAsynctask jsonasynctask = new jsonAsynctask(this);
  6.  
  7.  
  8. public DataTabelFragment() {
  9. // Required empty public constructor
  10. }
  11.  
  12. @Override
  13. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  14. // Inflate the layout for this fragment
  15.  
  16. View view = inflater.inflate( R.layout.fragment_data_tabel, container, false );
  17.  
  18. sensor1 = (TextView) view.findViewById( R.id.sensor1Box );
  19.  
  20. new jsonAsynctask(this).execute();
  21.  
  22. System.out.println("HEJ MED DIG");
  23.  
  24. return view;
  25.  
  26. }
  27.  
  28. public void inExecute() {
  29.  
  30. for (int i = 0; i < jsonasynctask.allId.size(); i++) {
  31.  
  32. sensor1.append( jsonasynctask.allId.get( i ) + " | " + jsonasynctask.allDevice.get( i ) + " | " + jsonasynctask.allTemp.get( i ) + " | " + jsonasynctask.allHum.get( i ) + " | " + jsonasynctask.allBat.get( i ) + " | " + jsonasynctask.allMode.get( i ) + " | " + jsonasynctask.allLux.get( i ) + " | " + jsonasynctask.allDate_time.get( i ) + "nn" );
  33.  
  34. }
  35.  
  36. }
  37.  
  38. }
  39.  
  40. public class jsonAsynctask extends AsyncTask<Void, Void, Void> {
  41.  
  42. DataTabelFragment dataTabelFragment;
  43.  
  44. JSONObject idArray, deviceArray, tempArray, humArray, batArray, modeArray, date_timeArray, luxArray;
  45. JSONArray json2;
  46.  
  47. String basicAuth, line, json_string, json, cxwebURL, credentials, password, username;
  48.  
  49. String data = "";
  50. String id = "";
  51.  
  52. List<String> allId = new ArrayList<String>();
  53. List<String> allDevice = new ArrayList<String>();
  54. List<String> allTemp = new ArrayList<String>();
  55. List<String> allHum = new ArrayList<String>();
  56. List<String> allBat = new ArrayList<String>();
  57. List<String> allMode = new ArrayList<String>();
  58. List<String> allDate_time = new ArrayList<String>();
  59. List<String> allLux = new ArrayList<String>();
  60.  
  61. Gson gson;
  62.  
  63. ProgressDialog pd;
  64.  
  65. //HttpsURLConnection connection;
  66. HttpURLConnection connection;
  67. BufferedReader bufferedReader;
  68.  
  69. URL url;
  70.  
  71.  
  72. public jsonAsynctask(DataTabelFragment dataTabelFragment) {
  73. this.dataTabelFragment = dataTabelFragment;
  74. }
  75.  
  76. public void inBackground() {
  77.  
  78. username = "xxx";
  79. password = "xxx";
  80.  
  81. credentials = username + ":" + password;
  82.  
  83. cxwebURL = "https://" + credentials + "@xxx.com/fetch.php?";
  84.  
  85. try {
  86.  
  87. url = new URL( cxwebURL );
  88.  
  89. connection = (HttpsURLConnection) url.openConnection();
  90.  
  91. basicAuth = "Basic " + new String( encodeBase64URLSafeString( credentials.getBytes() ) );
  92.  
  93. connection.setRequestProperty( "Authorization", basicAuth );
  94. connection.setRequestMethod( "GET" );
  95. connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
  96. connection.setRequestProperty( "Content-Language", "en-US" );
  97. connection.setUseCaches( false );
  98. connection.setDoInput( true );
  99. connection.setDoOutput( true );
  100. connection.connect();
  101.  
  102. InputStream stream = connection.getInputStream();
  103.  
  104. bufferedReader = new BufferedReader( new InputStreamReader( stream ) );
  105.  
  106. line = "";
  107.  
  108. while (line != null) {
  109. line = bufferedReader.readLine();
  110. data = data + line;
  111. }
  112.  
  113. System.out.println( "PRINT DATA HER: " + data );
  114.  
  115. json2 = new JSONArray( data );
  116.  
  117. System.out.println( "DET HER ER json2" + json2 );
  118.  
  119. for (int i = 0; i < json2.length(); i++) {
  120. idArray = json2.getJSONObject( i );
  121. deviceArray = json2.getJSONObject( i );
  122. tempArray = json2.getJSONObject( i );
  123. humArray = json2.getJSONObject( i );
  124. batArray = json2.getJSONObject( i );
  125. modeArray = json2.getJSONObject( i );
  126. date_timeArray = json2.getJSONObject( i );
  127. luxArray = json2.getJSONObject( i );
  128. id = idArray.getString( "id" );
  129.  
  130. String temp = tempArray.getString( "temp" );
  131. String device = deviceArray.getString( "device" );
  132. String hum = humArray.getString( "hum" );
  133. String bat = batArray.getString( "bat" );
  134. String mode = modeArray.getString( "mode" );
  135. String date_time = date_timeArray.getString( "time" );
  136. String lux = luxArray.getString( "light" );
  137.  
  138. allId.add( id );
  139. allDevice.add( device );
  140. allTemp.add( temp );
  141. allHum.add( hum );
  142. allBat.add( bat );
  143. allMode.add( mode );
  144. allDate_time.add( date_time );
  145. allLux.add( lux );
  146.  
  147. }
  148.  
  149. } catch (MalformedURLException e) {
  150. e.printStackTrace();
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. } catch (JSONException e) {
  154. e.printStackTrace();
  155. }
  156.  
  157. }
  158.  
  159. private static String encodeBase64URLSafeString(byte[] binaryData) {
  160.  
  161. return android.util.Base64.encodeToString( binaryData, android.util.Base64.URL_SAFE );
  162.  
  163. }
  164.  
  165. @Override
  166. protected Void doInBackground(Void... voids) {
  167.  
  168. inBackground();
  169.  
  170. return null;
  171. }
  172.  
  173. @Override
  174. protected void onPreExecute() {
  175. super.onPreExecute();
  176.  
  177. //pd = new ProgressDialog( new MainActivity() );
  178. //pd.setMessage( "Være sød at vente" );
  179. //pd.setCancelable( false );
  180. //pd.show();
  181. }
  182.  
  183. @Override
  184. public void onPostExecute(Void result) {
  185. super.onPostExecute( result );
  186. /*
  187. if (pd.isShowing()) {
  188. pd.dismiss();
  189. }*/
  190.  
  191. gson = new Gson();
  192.  
  193. json = gson.toJson( data );
  194.  
  195. json_string = data;
  196.  
  197. dataTabelFragment.inExecute();
  198. }
  199. }
Add Comment
Please, Sign In to add comment