zhack000

parsingJSON

Aug 21st, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.16 KB | None | 0 0
  1. package com.example.robot.fastboatd;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.webkit.WebView;
  8. import android.widget.Button;
  9. import android.widget.ListAdapter;
  10. import android.widget.ListView;
  11. import android.widget.SimpleAdapter;
  12. import android.widget.Toast;
  13.  
  14. import org.json.JSONArray;
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17.  
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20.  
  21. /**
  22.  * Created by ROBOT on 19/08/2017.
  23.  */
  24.  
  25. public class ParsingJSON extends MainActivity {
  26.     private ProgressDialog pDialog;
  27.     private ListView lv,lvx;
  28.     private String TAG = MainActivity.class.getSimpleName();
  29.     public String l;
  30.     public Bundle bandelz;
  31.     public Button bton;
  32.     public String alamat;
  33.     public WebView wv;
  34.     ArrayList<HashMap<String, String>> dataList;
  35.     protected void onCreate(Bundle savedInstanceState){
  36.         super.onCreate(savedInstanceState);
  37.         setContentView(R.layout.semogafix);
  38.         dataList = new ArrayList<>();
  39.         bandelz = getIntent().getExtras();
  40.         lv = (ListView) findViewById(R.id.list);
  41.         new GetContacts().execute();
  42.         bton = (Button) findViewById(R.id.button);
  43.         wv = (WebView) findViewById(R.id.webv);
  44.     }
  45.     public class GetContacts extends AsyncTask<Void, Void, Void> {
  46.  
  47.  
  48.         @Override
  49.         public void onPreExecute() {
  50.             super.onPreExecute();
  51.             // Showing progress dialog
  52.             pDialog = new ProgressDialog(ParsingJSON.this);
  53.             pDialog.setMessage("Please wait...");
  54.             pDialog.setCancelable(true);
  55.  
  56.             pDialog.show();
  57.  
  58.         }
  59.  
  60.         @Override
  61.         public Void doInBackground(Void... arg0) {
  62.             HttpHandler sh = new HttpHandler();
  63.             // Making a request to url and getting response
  64.             l = bandelz.getString("ling");
  65.             String jsonStr = sh.makeServiceCall(l);
  66.  
  67.             Log.e(TAG, "Response from url: " + jsonStr);
  68.  
  69.             if (jsonStr != null) {
  70.                 try {
  71.                     JSONObject jsonObj = new JSONObject(jsonStr);
  72.  
  73.                     // Getting JSON Array node
  74.                     JSONArray contacts = jsonObj.getJSONArray("results");
  75.                     ArrayList<HashMap> datam = new ArrayList<>();
  76.                     // looping through All Contacts
  77.                     for (int i = 0; i < contacts.length(); i++) {
  78.                         JSONObject c = contacts.getJSONObject(i);
  79.                         String boat = c.getString("boat");
  80.                         String name = c.getString("agent");
  81.                         String price = c.getString("price");
  82.                         String agent_site = c.getString("_agent_site");
  83.                         /*HashMap<String,String> datan = new HashMap<>();
  84.                         datan.put("boat",boat);
  85.                         datan.put("name",name);
  86.                         datan.put("price",price);*/
  87.  
  88.  
  89.                         int awal=0;
  90.  
  91.  
  92.                        HashMap<String, String> contact = new HashMap<>();
  93.                          //   if(contact.get("boat")== bisadong[0]){
  94.                                contact.put("boat",boat);
  95.                               contact.put("name", name);
  96.                         contact.put("price", price);
  97.                             //contact.put("boats", boat);
  98.  
  99.  
  100.                         dataList.add(contact);
  101.  
  102.                     }
  103.  
  104.                 } catch (final JSONException e) {
  105.                     Log.e(TAG, "Json parsing error: " + e.getMessage());
  106.                     runOnUiThread(new Runnable() {
  107.                         @Override
  108.                         public void run() {
  109.                             Toast.makeText(getApplicationContext(),
  110.                                     "Json parsing error: " + e.getMessage(),
  111.                                     Toast.LENGTH_LONG)
  112.                                     .show();
  113.                         }
  114.                     });
  115.  
  116.                 }
  117.             } else {
  118.                 runOnUiThread(new Runnable() {
  119.                     @Override
  120.                     public void run() {
  121.                         Toast.makeText(getApplicationContext(),
  122.                                 "Koneksi Internet tidak ditemukan, silahkan aktifkan koneksi anda dan coba kembali",
  123.                                 Toast.LENGTH_LONG)
  124.                                 .show();
  125.                     }
  126.                 });
  127.  
  128.             }
  129.  
  130.             return null;
  131.         }
  132.  
  133.  
  134.         @Override
  135.         protected void onPostExecute(Void result) {
  136.             super.onPostExecute(result);
  137.             // Dismiss the progress dialog
  138.             if (pDialog.isShowing())
  139.                 pDialog.dismiss();
  140.             /**
  141.              * Updating parsed JSON data into ListView
  142.              * */
  143.  
  144.             ListAdapter adapter = new SimpleAdapter(
  145.                     ParsingJSON.this, dataList,
  146.                     R.layout.list_item, new String[]{"boat","name","price"}, new int[]{R.id.boat,R.id.agent,R.id.price});
  147.             lv.setAdapter(adapter);
  148.         }
  149.  
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment