Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.matte.radialert;
- import android.app.ProgressDialog;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.GridView;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import com.sun.mail.iap.Response;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.params.BasicHttpParams;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.HashMap;
- public class Last100toGrid extends AppCompatActivity {
- String myJSON;
- private static final String TAG_RESULTS="result";
- private static final String TAG_ID = "id";
- private static final String TAG_VALUE = "value";
- private static final String TAG_DATE ="date";
- private static final String TAG_TIME = "time";
- private static final String TAG_ROOM = "room";
- private static final String TAG_FLAG = "flag";
- JSONArray rilevations = null;
- ArrayList<HashMap<String, String>> rilevationList;
- GridView gridView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_last100_relevations);
- gridView = (GridView) findViewById(R.id.gridView);
- rilevationList = new ArrayList<HashMap<String,String>>();
- getData();
- }
- protected void showList(){
- try {
- JSONObject jsonObj = new JSONObject(myJSON);
- rilevations = jsonObj.getJSONArray(TAG_RESULTS);
- for(int i=0;i<rilevations.length();i++){
- JSONObject c = rilevations.getJSONObject(i);
- String id = c.getString(TAG_ID);
- String value = c.getString(TAG_VALUE);
- String date = c.getString(TAG_DATE);
- String time = c.getString(TAG_TIME);
- String room = c.getString(TAG_ROOM);
- String flag = c.getString(TAG_FLAG);
- HashMap<String,String> persons = new HashMap<String,String>();
- persons.put(TAG_ID,id);
- persons.put(TAG_VALUE,value);
- persons.put(TAG_DATE,date);
- persons.put(TAG_TIME,time);
- persons.put(TAG_ROOM,room);
- persons.put(TAG_FLAG,flag);
- rilevationList.add(persons);
- }
- ListAdapter adapter = new SimpleAdapter(getBaseContext(), rilevationList, R.layout.datagrid_item,
- new String[]{TAG_ID, TAG_VALUE, TAG_DATE, TAG_TIME, TAG_ROOM, TAG_FLAG},
- 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}
- ) {
- };
- gridView.setAdapter(adapter);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- public void getData(){
- class GetDataJSON extends AsyncTask<String, Void, String> {
- @Override
- protected String doInBackground(String... params) {
- DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
- HttpPost httppost = new HttpPost("http://newparty-mr.com/ingegneria/last100.php");
- // Depends on your web service
- httppost.setHeader("Content-type", "application/json");
- InputStream inputStream = null;
- String result = null;
- try {
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity entity = response.getEntity();
- inputStream = entity.getContent();
- // json is UTF-8 by default
- BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null)
- {
- sb.append(line + "\n");
- }
- result = sb.toString();
- } catch (Exception e) {
- // Oops
- }
- finally {
- try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
- }
- return result;
- }
- @Override
- protected void onPostExecute(String result){
- myJSON=result;
- showList();
- }
- }
- GetDataJSON g = new GetDataJSON();
- g.execute();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement