搜尋此網誌

2013年10月8日 星期二

【Java】實作從html讀取JSON

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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;


public class JSONfunctions {

        public static JSONObject getJSONfromURL(String url, int method) {
                InputStream is = null;
                String result = "";
                JSONObject jObject = null;

                // http post or get
                try {
                        HttpClient httpclient = new DefaultHttpClient();

                        if (method == Globals.METHOD_GET) {
                                HttpGet httpget = new HttpGet(url);
                                HttpResponse response = httpclient.execute(httpget);
                                HttpEntity entity = response.getEntity();
                                is = entity.getContent();
                        }
                        else if( method == Globals.METHOD_POST) {
                                HttpPost httppost = new HttpPost(url);
                                HttpResponse response = httpclient.execute(httppost);
                                HttpEntity entity = response.getEntity();
                                is = entity.getContent();
                        }
                        

                } catch (Exception e) {
                 System.out.printf("log_tag:Error in http connection " + e.toString());
                }

                // convert response to string
                try {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(
                                        is, "utf-8"), 8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while ((line = reader.readLine()) != null) {
                                sb.append(line + "\n");
                        }
                        is.close();
                        result = sb.toString();
                        System.out.printf("RESULT: %s", result);
                } catch (Exception e) {
                 System.out.printf("log_tag:Error converting result " + e.toString());
                }

                try {

                        jObject = new JSONObject(result);
                } catch (JSONException e) {
                 System.out.printf("log_tag:Error parsing data " + e.toString());
                }

                return jObject;
        }
}
class Globals {
 public final static int METHOD_GET = 1;
    public final static int METHOD_POST = 2;
}
參考:

  1. http://www.androidbegin.com/tutorial/android-parsing-yql-using-json-tutorial/ 
  2. https://code.google.com/p/cabbiemagnet/source/browse/trunk/CabbieMagnetAndroid/src/com/cabbiemagnet/android/?r=27

沒有留言: