Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.robot.fastboatd;
- import android.app.ProgressDialog;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.webkit.WebView;
- import android.widget.Button;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.HashMap;
- /**
- * Created by ROBOT on 19/08/2017.
- */
- public class ParsingJSON extends MainActivity {
- private ProgressDialog pDialog;
- private ListView lv,lvx;
- private String TAG = MainActivity.class.getSimpleName();
- public String l;
- public Bundle bandelz;
- public Button bton;
- public String alamat;
- public WebView wv;
- ArrayList<HashMap<String, String>> dataList;
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.semogafix);
- dataList = new ArrayList<>();
- bandelz = getIntent().getExtras();
- lv = (ListView) findViewById(R.id.list);
- new GetContacts().execute();
- bton = (Button) findViewById(R.id.button);
- wv = (WebView) findViewById(R.id.webv);
- }
- public class GetContacts extends AsyncTask<Void, Void, Void> {
- @Override
- public void onPreExecute() {
- super.onPreExecute();
- // Showing progress dialog
- pDialog = new ProgressDialog(ParsingJSON.this);
- pDialog.setMessage("Please wait...");
- pDialog.setCancelable(true);
- pDialog.show();
- }
- @Override
- public Void doInBackground(Void... arg0) {
- HttpHandler sh = new HttpHandler();
- // Making a request to url and getting response
- l = bandelz.getString("ling");
- String jsonStr = sh.makeServiceCall(l);
- Log.e(TAG, "Response from url: " + jsonStr);
- if (jsonStr != null) {
- try {
- JSONObject jsonObj = new JSONObject(jsonStr);
- // Getting JSON Array node
- JSONArray contacts = jsonObj.getJSONArray("results");
- ArrayList<HashMap> datam = new ArrayList<>();
- // looping through All Contacts
- for (int i = 0; i < contacts.length(); i++) {
- JSONObject c = contacts.getJSONObject(i);
- String boat = c.getString("boat");
- String name = c.getString("agent");
- String price = c.getString("price");
- String agent_site = c.getString("_agent_site");
- /*HashMap<String,String> datan = new HashMap<>();
- datan.put("boat",boat);
- datan.put("name",name);
- datan.put("price",price);*/
- int awal=0;
- HashMap<String, String> contact = new HashMap<>();
- // if(contact.get("boat")== bisadong[0]){
- contact.put("boat",boat);
- contact.put("name", name);
- contact.put("price", price);
- //contact.put("boats", boat);
- dataList.add(contact);
- }
- } catch (final JSONException e) {
- Log.e(TAG, "Json parsing error: " + e.getMessage());
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(getApplicationContext(),
- "Json parsing error: " + e.getMessage(),
- Toast.LENGTH_LONG)
- .show();
- }
- });
- }
- } else {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(getApplicationContext(),
- "Koneksi Internet tidak ditemukan, silahkan aktifkan koneksi anda dan coba kembali",
- Toast.LENGTH_LONG)
- .show();
- }
- });
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- super.onPostExecute(result);
- // Dismiss the progress dialog
- if (pDialog.isShowing())
- pDialog.dismiss();
- /**
- * Updating parsed JSON data into ListView
- * */
- ListAdapter adapter = new SimpleAdapter(
- ParsingJSON.this, dataList,
- R.layout.list_item, new String[]{"boat","name","price"}, new int[]{R.id.boat,R.id.agent,R.id.price});
- lv.setAdapter(adapter);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment