2012-05-14 89 views
0

我有一個問題,而我從服務器中獲取數據的文本我不能轉換成json對象不能轉換成json數組我只是從數據庫中獲取標題和作者,並顯示在列表視圖json格式異步任務問題

{"document":[{"id":"1","title":"complete refrence of android", 
"author":"parag vyas","description":"lnvkzxhbkgbovdghognsdkhogjhlldnglj"}]} 

plz幫助我

public class showalbooks extends Activity { 
ArrayList<String> mylist = new ArrayList<String>(); 
String returnString=""; 

    ListView listView ; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.showalbooks); 

    listView = (ListView) findViewById(R.id.mylist); 
    new LongRunningGetIO().execute(); 


} 

private class LongRunningGetIO extends AsyncTask <Void, Void, String> { 

    protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException { 
     InputStream in = entity.getContent(); 
     StringBuffer out = new StringBuffer(); 
     int n = 1; 
     while (n>0) { 
      byte[] b = new byte[4096]; 
      n = in.read(b); 
      if (n>0) out.append(new String(b, 0, n)); 
     } 
     return out.toString(); 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpGet httpGet = new HttpGet("http://192.168.1.156/recess/document/document.json"); 
     HttpClient client = new DefaultHttpClient(); 
     HttpResponse response=null; 
     try{ 
      response = client.execute(httpGet); 
      } 
     catch(Exception e){} 
     System.out.println(response.getStatusLine()); 
     String text = null; 
     try { 
       response = httpClient.execute(httpGet, localContext); 
       HttpEntity entity = response.getEntity(); 
       text = getASCIIContentFromEntity(entity); 

     } catch (Exception e) { 
      return e.getLocalizedMessage(); 
     } 
       String var =text;    
       try{ 
        JSONArray jArray = new JSONArray(var); 
        for(int i=0;i<jArray.length();i++){ 

          JSONObject json_data = jArray.getJSONObject(i); 
          Log.i("log_tag","id: "+json_data.getString("id")+ 
            ", title: "+json_data.getString("title") 
         ); 
          returnString += "\n" +"id:"+ json_data.getString("id")+" "+"Title:"+ json_data.getString("title"); 

          } 


      } 
      catch(JSONException e){ 
        Log.e("log_tag", "Error parsing data "+e.toString()); 
      } 

       listView.setFilterText(returnString); 
      return returnString; 
    } 
    protected void onPostExecute(String results) { 
     if (results!=null) { 


      listView.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> arg0, View listview, 
         int documentid, long documenttitle) { 
        // TODO Auto-generated method stub 

       } 
     }); 
     } 

}}} 

回答

1

的JSON陣列由JSON對象封閉,並有ID爲 「文件」。的

代替:

JSONArray jArray = new JSONArray(var); 

你應該有:

JSONObject jObj = new JSONObject(var); 
JSONArray jArray = jObj.getJSONArray("document"); 
+0

我取代它,但它告訴我黑屏當我運行 – Mohit

+0

那麼你或許應該設置在視圖中的文本'onPostExecute '... – MByD

+0

雅一半的問題已解決謝謝binyamin sharet和PLZ可以解決我的另一個問題,我只希望標題或作者如何過濾和顯示在列表視圖 – Mohit