Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DataTabelFragment extends Fragment {
- public TextView sensor1;
- jsonAsynctask jsonasynctask = new jsonAsynctask(this);
- public DataTabelFragment() {
- // Required empty public constructor
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View view = inflater.inflate( R.layout.fragment_data_tabel, container, false );
- sensor1 = (TextView) view.findViewById( R.id.sensor1Box );
- new jsonAsynctask(this).execute();
- System.out.println("HEJ MED DIG");
- return view;
- }
- public void inExecute() {
- for (int i = 0; i < jsonasynctask.allId.size(); i++) {
- 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" );
- }
- }
- }
- public class jsonAsynctask extends AsyncTask<Void, Void, Void> {
- DataTabelFragment dataTabelFragment;
- JSONObject idArray, deviceArray, tempArray, humArray, batArray, modeArray, date_timeArray, luxArray;
- JSONArray json2;
- String basicAuth, line, json_string, json, cxwebURL, credentials, password, username;
- String data = "";
- String id = "";
- List<String> allId = new ArrayList<String>();
- List<String> allDevice = new ArrayList<String>();
- List<String> allTemp = new ArrayList<String>();
- List<String> allHum = new ArrayList<String>();
- List<String> allBat = new ArrayList<String>();
- List<String> allMode = new ArrayList<String>();
- List<String> allDate_time = new ArrayList<String>();
- List<String> allLux = new ArrayList<String>();
- Gson gson;
- ProgressDialog pd;
- //HttpsURLConnection connection;
- HttpURLConnection connection;
- BufferedReader bufferedReader;
- URL url;
- public jsonAsynctask(DataTabelFragment dataTabelFragment) {
- this.dataTabelFragment = dataTabelFragment;
- }
- public void inBackground() {
- username = "xxx";
- password = "xxx";
- credentials = username + ":" + password;
- cxwebURL = "https://" + credentials + "@xxx.com/fetch.php?";
- try {
- url = new URL( cxwebURL );
- connection = (HttpsURLConnection) url.openConnection();
- basicAuth = "Basic " + new String( encodeBase64URLSafeString( credentials.getBytes() ) );
- connection.setRequestProperty( "Authorization", basicAuth );
- connection.setRequestMethod( "GET" );
- connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
- connection.setRequestProperty( "Content-Language", "en-US" );
- connection.setUseCaches( false );
- connection.setDoInput( true );
- connection.setDoOutput( true );
- connection.connect();
- InputStream stream = connection.getInputStream();
- bufferedReader = new BufferedReader( new InputStreamReader( stream ) );
- line = "";
- while (line != null) {
- line = bufferedReader.readLine();
- data = data + line;
- }
- System.out.println( "PRINT DATA HER: " + data );
- json2 = new JSONArray( data );
- System.out.println( "DET HER ER json2" + json2 );
- for (int i = 0; i < json2.length(); i++) {
- idArray = json2.getJSONObject( i );
- deviceArray = json2.getJSONObject( i );
- tempArray = json2.getJSONObject( i );
- humArray = json2.getJSONObject( i );
- batArray = json2.getJSONObject( i );
- modeArray = json2.getJSONObject( i );
- date_timeArray = json2.getJSONObject( i );
- luxArray = json2.getJSONObject( i );
- id = idArray.getString( "id" );
- String temp = tempArray.getString( "temp" );
- String device = deviceArray.getString( "device" );
- String hum = humArray.getString( "hum" );
- String bat = batArray.getString( "bat" );
- String mode = modeArray.getString( "mode" );
- String date_time = date_timeArray.getString( "time" );
- String lux = luxArray.getString( "light" );
- allId.add( id );
- allDevice.add( device );
- allTemp.add( temp );
- allHum.add( hum );
- allBat.add( bat );
- allMode.add( mode );
- allDate_time.add( date_time );
- allLux.add( lux );
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- private static String encodeBase64URLSafeString(byte[] binaryData) {
- return android.util.Base64.encodeToString( binaryData, android.util.Base64.URL_SAFE );
- }
- @Override
- protected Void doInBackground(Void... voids) {
- inBackground();
- return null;
- }
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- //pd = new ProgressDialog( new MainActivity() );
- //pd.setMessage( "Være sød at vente" );
- //pd.setCancelable( false );
- //pd.show();
- }
- @Override
- public void onPostExecute(Void result) {
- super.onPostExecute( result );
- /*
- if (pd.isShowing()) {
- pd.dismiss();
- }*/
- gson = new Gson();
- json = gson.toJson( data );
- json_string = data;
- dataTabelFragment.inExecute();
- }
- }
Add Comment
Please, Sign In to add comment