2012-05-14 4 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가 "문서"를 가지고있다.

대신 :

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

내가 그것을 대체하지만 난 – Mohit

+0

을 실행할 때 당신이 아마 onPostExecute'에서 뷰의 텍스트를 설정해야합니다 그럼 나에게 빈 화면을 보여

JSONArray jArray = new JSONArray(var); 

당신은해야한다 '... – MByD

+0

나중에 반 문제가 해결되었습니다. binyamin sharet와 plz는 제목이나 저자가 원하는 필터 만 목록보기에 표시 할 수있는 다른 문제를 해결할 수 있습니다. – Mohit