Android Open Source - Android-Parsing-YQL-using-JSON-Tutorial J S O Nfunctions






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;
// w  w w  .ja  va2s.  com
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONfunctions {

  public static JSONObject getJSONfromURL(String url){
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;
    
    // Download JSON data from URL
      try{
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost(url);
              HttpResponse response = httpclient.execute(httppost);
              HttpEntity entity = response.getEntity();
              is = entity.getContent();

      }catch(Exception e){
              Log.e("log_tag", "Error in http connection "+e.toString());
      }
      
      // Convert response to string
      try{
              BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
              }
              is.close();
              result=sb.toString();
      }catch(Exception e){
              Log.e("log_tag", "Error converting result "+e.toString());
      }
      
      try{
        
            jArray = new JSONObject(result);            
      }catch(JSONException e){
              Log.e("log_tag", "Error parsing data "+e.toString());
      }
    
      return jArray;
  }
}




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