0

我从服务器以文本形式获取数据时遇到问题我无法转换为 json 对象无法转换为 json 数组我只是从数据库中获取标题和作者并显示在列表视图中

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

请帮帮我

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

                }
        });
        }

}}}
4

1 回答 1

1

JSON 数组包含在 JSON 对象中,并具有 id “文档”。

代替:

JSONArray jArray = new JSONArray(var);

你应该有:

JSONObject jObj = new JSONObject(var);
JSONArray jArray = jObj.getJSONArray("document");
于 2012-05-14T13:37:03.900 回答