Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.androidhive.jsonparsing;
- import android.app.ListActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.StrictMode;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.TextView;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.HashMap;
- public class AndroidJSONParsingActivity extends ListActivity {
- //URL to make request
- private static String url = "https://www.kimonolabs.com/api/2z7l9x5s?apikey=g1OfF7ZgyObIzGEACyu8G4KNCxD8ZrXp/";
- // JSON Node names
- private static final String TAG_NAME = "name";
- private static final String TAG_COUNT = "count";
- private static final String TAG_FREQUENCY = "frequency";
- private static final String TAG_VERSION = "version";
- private static final String TAG_NEWDATA = "newdata";
- private static final String TAG_LASTRUNSTATUS = "lasturnstatus";
- private static final String TAG_LASTSUCCESS = "lastsuccess";
- private static final String TAG_THISVERSIONRUN = "thisversionrun";
- private static final String TAG_THISVERSIONSTATUS = "thisversionstatus";
- private static final String TAG_NEXTRUN = "nextrun";
- private static final String TAG_RESULTS = "results";
- private static final String TAG_COLLECTION = "collection1";
- private static final String TAG_PROPERTY = "property1";
- private static final String TAG_TEXT = "text";
- private static final String TAG_HREF = "href";
- // contacts JSONArray
- JSONArray collection1 = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //Allows to run http in UI thread
- StrictMode.ThreadPolicy policy = new StrictMode.
- ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
- // Hashmap for ListView
- ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
- // Creating JSON Parser instance
- JSONParser jParser = new JSONParser();
- // getting JSON string from URL
- JSONObject json = jParser.getJSONFromUrl(url);
- try {
- String name = json.getString(TAG_NAME);
- Integer count = json.getInt(TAG_COUNT);
- String frequency = json.getString(TAG_FREQUENCY);
- Integer version = json.getInt(TAG_VERSION);
- Boolean newdata = json.getBoolean(TAG_NEWDATA);
- String lastrunstatus = json.getString(TAG_LASTRUNSTATUS);
- String lastsuccess = json.getString(TAG_LASTSUCCESS);
- String thisversionrun = json.getString(TAG_THISVERSIONRUN);
- String thisversionstatus = json.getString(TAG_THISVERSIONSTATUS);
- String nextrun = json.getString(TAG_NEXTRUN);
- JSONObject results = json.getJSONObject(TAG_RESULTS);
- collection1 = results.getJSONArray(TAG_COLLECTION);
- // looping through the property
- for (int i = 0; i < collection1.length(); i++){
- JSONObject propertyobject = collection1.getJSONObject(i);
- JSONObject property = propertyobject.getJSONObject(TAG_PROPERTY);
- // Storing each json item in variable
- String text = property.getString(TAG_TEXT);
- String href = property.getString(TAG_HREF);
- //creating new HashMap
- HashMap<String, String> map = new HashMap<String, String>();
- //adding each child node to HashMap key => value
- map.put(TAG_TEXT, text);
- map.put(TAG_HREF, href);
- // bar.setProgress(y);
- contactList.add(map);
- }
- } catch (JSONException e){
- e.printStackTrace();
- }
- ListAdapter adapter = new SimpleAdapter(AndroidJSONParsingActivity.this, contactList,
- R.layout.list_item,
- new String[]{TAG_HREF, TAG_TEXT}, new int[]{
- R.id.name, R.id.email}
- );
- setListAdapter(adapter);
- // selecting single ListView item
- ListView lv = getListView();
- // Launching new screen on Selecting Single ListItem
- lv.setOnItemClickListener(new
- OnItemClickListener()
- {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id)
- {
- //getting values from selected ListItem
- //String property = ((TextView) view.findViewById(R.id.name)).getText().toString();
- String text = ((TextView) view.findViewById(R.id.email)).getText().toString();
- String href = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
- // Starting new intent
- Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
- in.putExtra(TAG_TEXT, text);
- in.putExtra(TAG_HREF, href);
- startActivity(in);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement