Advertisement
Guest User

Untitled

a guest
May 25th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. package com.example.matte.radialert;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.os.AsyncTask;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.widget.GridView;
  10. import android.widget.ListAdapter;
  11. import android.widget.ListView;
  12. import android.widget.SimpleAdapter;
  13.  
  14. import com.sun.mail.iap.Response;
  15.  
  16. import org.apache.http.HttpEntity;
  17. import org.apache.http.HttpResponse;
  18. import org.apache.http.client.methods.HttpPost;
  19. import org.apache.http.impl.client.DefaultHttpClient;
  20. import org.apache.http.params.BasicHttpParams;
  21. import org.json.JSONArray;
  22. import org.json.JSONException;
  23. import org.json.JSONObject;
  24.  
  25. import java.io.BufferedReader;
  26. import java.io.InputStream;
  27. import java.io.InputStreamReader;
  28. import java.util.ArrayList;
  29. import java.util.HashMap;
  30.  
  31. public class Last100toGrid extends AppCompatActivity {
  32.  
  33. String myJSON;
  34. private static final String TAG_RESULTS="result";
  35. private static final String TAG_ID = "id";
  36. private static final String TAG_VALUE = "value";
  37. private static final String TAG_DATE ="date";
  38. private static final String TAG_TIME = "time";
  39. private static final String TAG_ROOM = "room";
  40. private static final String TAG_FLAG = "flag";
  41.  
  42. JSONArray rilevations = null;
  43.  
  44. ArrayList<HashMap<String, String>> rilevationList;
  45.  
  46. GridView gridView;
  47.  
  48. @Override
  49. protected void onCreate(Bundle savedInstanceState) {
  50. super.onCreate(savedInstanceState);
  51. setContentView(R.layout.activity_last100_relevations);
  52. gridView = (GridView) findViewById(R.id.gridView);
  53. rilevationList = new ArrayList<HashMap<String,String>>();
  54. getData();
  55.  
  56. }
  57.  
  58.  
  59. protected void showList(){
  60. try {
  61. JSONObject jsonObj = new JSONObject(myJSON);
  62. rilevations = jsonObj.getJSONArray(TAG_RESULTS);
  63.  
  64. for(int i=0;i<rilevations.length();i++){
  65. JSONObject c = rilevations.getJSONObject(i);
  66. String id = c.getString(TAG_ID);
  67. String value = c.getString(TAG_VALUE);
  68. String date = c.getString(TAG_DATE);
  69. String time = c.getString(TAG_TIME);
  70. String room = c.getString(TAG_ROOM);
  71. String flag = c.getString(TAG_FLAG);
  72.  
  73. HashMap<String,String> persons = new HashMap<String,String>();
  74.  
  75. persons.put(TAG_ID,id);
  76. persons.put(TAG_VALUE,value);
  77. persons.put(TAG_DATE,date);
  78. persons.put(TAG_TIME,time);
  79. persons.put(TAG_ROOM,room);
  80. persons.put(TAG_FLAG,flag);
  81.  
  82. rilevationList.add(persons);
  83. }
  84.  
  85. ListAdapter adapter = new SimpleAdapter(getBaseContext(), rilevationList, R.layout.datagrid_item,
  86. new String[]{TAG_ID, TAG_VALUE, TAG_DATE, TAG_TIME, TAG_ROOM, TAG_FLAG},
  87. new int[]{R.id.id_gr, R.id.value_gr, R.id.date_gr, R.id.date_gr, R.id.time_gr, R.id.room_gr, R.id.flag}
  88. ) {
  89. };
  90.  
  91. gridView.setAdapter(adapter);
  92.  
  93. } catch (JSONException e) {
  94. e.printStackTrace();
  95. }
  96.  
  97. }
  98.  
  99. public void getData(){
  100. class GetDataJSON extends AsyncTask<String, Void, String> {
  101.  
  102. @Override
  103. protected String doInBackground(String... params) {
  104. DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
  105. HttpPost httppost = new HttpPost("http://newparty-mr.com/ingegneria/last100.php");
  106.  
  107. // Depends on your web service
  108. httppost.setHeader("Content-type", "application/json");
  109.  
  110. InputStream inputStream = null;
  111. String result = null;
  112. try {
  113. HttpResponse response = httpclient.execute(httppost);
  114. HttpEntity entity = response.getEntity();
  115.  
  116. inputStream = entity.getContent();
  117. // json is UTF-8 by default
  118. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
  119. StringBuilder sb = new StringBuilder();
  120.  
  121. String line = null;
  122. while ((line = reader.readLine()) != null)
  123. {
  124. sb.append(line + "\n");
  125. }
  126. result = sb.toString();
  127. } catch (Exception e) {
  128. // Oops
  129. }
  130. finally {
  131. try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
  132. }
  133. return result;
  134. }
  135.  
  136. @Override
  137. protected void onPostExecute(String result){
  138. myJSON=result;
  139. showList();
  140. }
  141. }
  142. GetDataJSON g = new GetDataJSON();
  143. g.execute();
  144. }
  145.  
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement