0

i have a problem while im get the data from the server in text i can't converted into json object can not be converted into json array i just get the title and author from the database and show in list view

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

plz help me

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 Answer 1

1

The JSON array is enclosed by a JSON object, and has the id "document".

instead of:

JSONArray jArray = new JSONArray(var); 

You should have:

JSONObject jObj = new JSONObject(var); JSONArray jArray = jObj.getJSONArray("document"); 
Sign up to request clarification or add additional context in comments.

3 Comments

ya the half problem is resolved thanks binyamin sharet and plz can u solved my another problem that i only want title or author how can i filter and show in list view
This is not that clear, try solving it and if you fail to do so, you can ask another question in StackOverflow.
If the answer solved your problem, please accept it by clicking the tick (under the scores)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.