[android] Android JSON Parsing Client

本帖最後由 DraGoNxHK 於 2012-5-24 23:37 編輯

//咁如果我要係一個普通activity裡面行到setListAdapter()應該點做?
//thanks~ (solved)

public static JSONObject getJSONfromURL(String url)
    {
        //initialize
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        //http post
        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 parse the string to a JSON object
        try{
                jArray = new JSONObject(result);
        }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
     
        return jArray;
      
    }

D code上網搵既..
睇返logcat係parsing data果度出事..
其實會唔會係我個json arrray同人唔同?
json array內容可見#6..(created by php)

你都唔係 ListViewActivity, 咁你想個 setListAdapter() set 去邊?
你可以 create 個 ListView 黎俾佢 setAdaptor() o既.

TOP

你都唔係 ListViewActivity, 咁你想個 setListAdapter() set 去邊?
你可以 create 個 ListView 黎俾佢 setA ...
IanW 發表於 2012-5-24 11:18


ic..成功既..
但係如果我想有個咁既structure:
ScrollView(TableLayout (TableRow1: TabWidget; TableRow2: Spinner; TableRow3: ListView)
不過我見到佢話ScrollView裡面唔應該有ListView..
基於設計方面,,我唔想開多個Activity..會轉唔到Tab...

TOP

本帖最後由 神秘二代 於 2012-5-24 18:02 編輯

Programer名言: 警告即沒問題,只要沒錯誤就好了~

TOP

想信最大問題會係有可能 scroll 到只係出倒你個 listview, 你 scroll 唔返 scrollview 入面o既其他野啫.

TOP

咁即係唔關listview放邊事..

我整極都係出java.lang.string cannot be converted to JSONArray
String result既整法同以下result個整法定全一樣..

http://www.helloandroid.com/tutorials/connecting-mysql-database
之後出事既應該係:::JSONArray jarray = new JSONArray(result);

補充返我json既形式係咁既..
[{"ID":"1","Name":"AAA","Class":"AA","Language":"English","Length":"111"},{"ID":"2","Name":"BBB","Class":"BB","Language":"English","Length":"222"},{"ID":"3","Name":"CCC","Class":"CC","Language":"English","Length":"333"}]

TOP

Remember that you should use a thread to make the HTTP connection. Take a look at AsyncTask.

Google Gson is a good JSON parser that supports Java reflection, you may want to give it a shot.

TOP

Remember that you should use a thread to make the HTTP connection. Take a look at AsyncTask.

Google ...
無忌 發表於 2012-5-25 00:19


ya..the function is inside AsyncTask's doInBackground()..
i know gson is an alternative but it needs to be downloaded as a plugin..
i just wonder why I cannot just apply the example done on the web.

TOP

本帖最後由 DraGoNxHK 於 2012-5-27 01:26 編輯

我試用過好多唔同方法都係唔得..
第一個方法係最基本果個json parsing方法,網上流傳既version基本上一樣..
第二個方法係用gson..跟足哂網上教學..但都係failed..
最後我搵返有本書有講json parsing..唔需要用gson..個flow唔同第一個..
不過都係failed..

咁多次failure既原因其實係一樣:
org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray
最後原來發現係我php gen個code多左D header,title...

下一步係要android client send返野比php update database..
係咪都一樣用返json object傳去php比php解讀?
or應該直接send infomation e.g. string比php?

TOP

回復 9# DraGoNxHK


    既然確定係 php 出問題,點解唔 post 你段 php code

TOP