Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.41 KB | None | 0 0
  1. package com.androidhive.jsonparsing;
  2.  
  3. import android.app.ListActivity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.os.StrictMode;
  7.  
  8. import android.view.View;
  9. import android.widget.AdapterView;
  10. import android.widget.AdapterView.OnItemClickListener;
  11. import android.widget.ListAdapter;
  12. import android.widget.ListView;
  13. import android.widget.SimpleAdapter;
  14. import android.widget.TextView;
  15.  
  16. import org.json.JSONArray;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22.  
  23.  
  24.  public class AndroidJSONParsingActivity extends ListActivity {
  25.  
  26.      //URL to make request
  27.      private static String url = "https://www.kimonolabs.com/api/2z7l9x5s?apikey=g1OfF7ZgyObIzGEACyu8G4KNCxD8ZrXp/";
  28.  
  29.      // JSON Node names
  30.      private static final String TAG_NAME = "name";
  31.      private static final String TAG_COUNT = "count";
  32.      private static final String TAG_FREQUENCY = "frequency";
  33.      private static final String TAG_VERSION = "version";
  34.      private static final String TAG_NEWDATA = "newdata";
  35.      private static final String TAG_LASTRUNSTATUS = "lasturnstatus";
  36.      private static final String TAG_LASTSUCCESS = "lastsuccess";
  37.      private static final String TAG_THISVERSIONRUN = "thisversionrun";
  38.      private static final String TAG_THISVERSIONSTATUS = "thisversionstatus";
  39.      private static final String TAG_NEXTRUN = "nextrun";
  40.  
  41.  
  42.      private static final String TAG_RESULTS = "results";
  43.      private static final String TAG_COLLECTION = "collection1";
  44.      private static final String TAG_PROPERTY = "property1";
  45.      private static final String TAG_TEXT = "text";
  46.      private static final String TAG_HREF = "href";
  47.      // contacts JSONArray
  48.      JSONArray collection1 = null;
  49.  
  50.  
  51.      @Override
  52.      public void onCreate(Bundle savedInstanceState) {
  53.          super.onCreate(savedInstanceState);
  54.          setContentView(R.layout.main);
  55.  
  56.          //Allows to run http in UI thread
  57.          StrictMode.ThreadPolicy policy = new StrictMode.
  58.                  ThreadPolicy.Builder().permitAll().build();
  59.          StrictMode.setThreadPolicy(policy);
  60.  
  61.  
  62.          // Hashmap for ListView
  63.          ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
  64.          // Creating JSON Parser instance
  65.          JSONParser jParser = new JSONParser();
  66.          // getting JSON string from URL
  67.          JSONObject json = jParser.getJSONFromUrl(url);
  68.  
  69.  
  70.          try {
  71.              String name = json.getString(TAG_NAME);
  72.              Integer count = json.getInt(TAG_COUNT);
  73.              String frequency = json.getString(TAG_FREQUENCY);
  74.              Integer version = json.getInt(TAG_VERSION);
  75.              Boolean newdata = json.getBoolean(TAG_NEWDATA);
  76.              String lastrunstatus = json.getString(TAG_LASTRUNSTATUS);
  77.              String lastsuccess = json.getString(TAG_LASTSUCCESS);
  78.              String thisversionrun = json.getString(TAG_THISVERSIONRUN);
  79.              String thisversionstatus = json.getString(TAG_THISVERSIONSTATUS);
  80.              String nextrun = json.getString(TAG_NEXTRUN);
  81.  
  82.              JSONObject results = json.getJSONObject(TAG_RESULTS);
  83.              collection1 = results.getJSONArray(TAG_COLLECTION);
  84.  
  85.              // looping through the property
  86.              for (int i = 0; i < collection1.length(); i++){
  87.                  JSONObject propertyobject = collection1.getJSONObject(i);
  88.  
  89.                  JSONObject property = propertyobject.getJSONObject(TAG_PROPERTY);
  90.  
  91.                      // Storing each json item in variable
  92.                      String text = property.getString(TAG_TEXT);
  93.                      String href = property.getString(TAG_HREF);
  94.  
  95.                      //creating new HashMap
  96.                      HashMap<String, String> map = new HashMap<String, String>();
  97.  
  98.                      //adding each child node to HashMap key => value
  99.                      map.put(TAG_TEXT, text);
  100.                      map.put(TAG_HREF, href);
  101.  
  102.                      //  bar.setProgress(y);
  103.                      contactList.add(map);
  104.  
  105.              }
  106.          } catch (JSONException e){
  107.              e.printStackTrace();
  108.          }
  109.  
  110.  
  111.          ListAdapter adapter = new SimpleAdapter(AndroidJSONParsingActivity.this, contactList,
  112.                  R.layout.list_item,
  113.                  new String[]{TAG_HREF, TAG_TEXT}, new int[]{
  114.                  R.id.name, R.id.email}
  115.          );
  116.  
  117.          setListAdapter(adapter);
  118.          // selecting single ListView item
  119.          ListView lv = getListView();
  120.          // Launching new screen on Selecting Single ListItem
  121.          lv.setOnItemClickListener(new
  122.  
  123.          OnItemClickListener()
  124.          {
  125.          @Override
  126.          public void onItemClick(AdapterView<?> parent, View view,
  127.                  int position, long id)
  128.          {
  129.          //getting values from selected ListItem
  130.          //String property = ((TextView) view.findViewById(R.id.name)).getText().toString();
  131.          String text = ((TextView) view.findViewById(R.id.email)).getText().toString();
  132.          String href = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
  133.  
  134.          // Starting new intent
  135.          Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
  136.          in.putExtra(TAG_TEXT, text);
  137.          in.putExtra(TAG_HREF, href);
  138.          startActivity(in);
  139.          }
  140.          });
  141.      }
  142.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement