0
@Override protected Integer doInBackground(Void... params) { String readMyJSON = readMyJSON(); try { JSONArray jsonArray = new JSONArray(readMyJSON); } catch (Exception e) { e.printStackTrace(); } return 0; } 

This is not working, during JSONArray jsonArray = new JSONArray(readMyJSON);, JSONException occurs. Could somebody tell me where is the problem? Thanks in advance.

P.S.: Method I use:

 public String readMyJSON() { StringBuilder builder = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://search.twitter.com/search.json?q=android"); try { HttpResponse response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; while ((line = reader.readLine()) != null) { builder.append(line); } } else { Log.e(ParseJSON.class.toString(), "Failed to download file"); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return builder.toString(); } 

This method seems to be allright.

1
  • What do you get for output from readMyJSON()? Commented Feb 23, 2013 at 22:18

2 Answers 2

2

In short, the response from twitter is not an JSON array, so you get an exception. If you want the array of results, do the following:

JSONObject object = (JSONObject) new JSONTokener(json).nextValue(); JSONArray results = object.getJSONArray("results"); 

I suggest you read the Twitter API docs. Specifically, https://dev.twitter.com/docs/api/1/get/search.

Sign up to request clarification or add additional context in comments.

1 Comment

I wanted to do this for the purpose of learning how to work with JSON. I used this search result only as an example. It seems that it´s working now, thanks.
0

Can you try this:

JSONArray jsonArray = new JSONArray("["+readMyJSON+"]"); 

I hope it should work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.