Android Open Source - Android-Parsing-YQL-using-JSON-Tutorial Main Activity






From Project

Back to project page Android-Parsing-YQL-using-JSON-Tutorial.

License

The source code is released under:

Apache License

If you think the Android project Android-Parsing-YQL-using-JSON-Tutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.yqltutorial;
//  ww w . j a v a2 s  . c  om
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class MainActivity extends Activity {
  // Declare Variables
  ListView listview;
  ListViewAdapter adapter;
  ProgressDialog mProgressDialog;
  ArrayList<HashMap<String, String>> arraylist;
  static String TWEETS = "tweets";
  static String PIMAGE = "pimage";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from listview_main.xml
    setContentView(R.layout.listview_main);
    // Execute DownloadJSON AsyncTask
    new DownloadJSON().execute();
  }

  // DownloadJSON AsyncTask
  private class DownloadJSON extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      // Create a progressdialog
      mProgressDialog = new ProgressDialog(MainActivity.this);
      // Set progressdialog title
      mProgressDialog.setTitle("Android Parsing YQL in JSON Tutorial");
      // Set progressdialog message
      mProgressDialog.setMessage("Loading...");
      mProgressDialog.setIndeterminate(false);
      // Show progressdialog
      mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
      // Create the array
      arraylist = new ArrayList<HashMap<String, String>>();
      // YQL JSON URL
      String url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D%22http%3A%2F%2Fsearch.twitter.com%2Fsearch.json%3Fq%3Dandroid%22&format=json&callback=";

      try {
        // Retrive JSON Objects from the given URL in JSONfunctions.class
        JSONObject json_data = JSONfunctions.getJSONfromURL(url);
        JSONObject json_query = json_data.getJSONObject("query");
        JSONObject json_results = json_query.getJSONObject("results");
        JSONObject json_json_results = json_results.getJSONObject("json");
        JSONArray json_array_result = json_json_results.getJSONArray("results");

        for (int i = 0; i < json_array_result.length(); i++) {
          JSONObject c = json_array_result.getJSONObject(i);
          HashMap<String, String> map = new HashMap<String, String>();
          // Retrive JSON Objects
          map.put("tweets", c.getString("text").toString());
          map.put("pimage", c.getString("profile_image_url").toString());
          // Set the JSON Objects into the array
          arraylist.add(map);
        }

      } catch (JSONException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
      }
      return null;
    }

    @Override
    protected void onPostExecute(Void args) {
      // Locate the listview in listview_main.xml
      listview = (ListView) findViewById(R.id.listview);
      // Pass the results into ListViewAdapter.java
      adapter = new ListViewAdapter(MainActivity.this, arraylist);
      // Binds the Adapter to the ListView
      listview.setAdapter(adapter);
      // Close the progressdialog
      mProgressDialog.dismiss();
    }
  }
}




Java Source Code List

com.androidbegin.yqltutorial.FileCache.java
com.androidbegin.yqltutorial.ImageLoader.java
com.androidbegin.yqltutorial.JSONfunctions.java
com.androidbegin.yqltutorial.ListViewAdapter.java
com.androidbegin.yqltutorial.MainActivity.java
com.androidbegin.yqltutorial.MemoryCache.java
com.androidbegin.yqltutorial.SingleItemView.java
com.androidbegin.yqltutorial.Utils.java
com.example.yqltutorial.FileCache.java
com.example.yqltutorial.ImageLoader.java
com.example.yqltutorial.JSONfunctions.java
com.example.yqltutorial.ListViewAdapter.java
com.example.yqltutorial.MainActivity.java
com.example.yqltutorial.MemoryCache.java
com.example.yqltutorial.SingleItemView.java
com.example.yqltutorial.Utils.java