1

in Android while parsing JSON, it parse only 10 items of JSON array. while debuging i found out that query string after connection.openConnection() ; returning null query String for example http://www.example.com/recent_summary/?count=20 it doesnot matter if i decrease the count to 5 it still return 10 items. but, when i browse the URL in browner. it return all 20 items .. i thing its something wrong with my code.. server side working fine and one more thing that when i was checking openConnection() declaration(URL.java) in android Studio i found lot of red lines .. noob here dnt know whether it will help or not but Please help me out.

public class MainListActivity extends ActionBarActivity { protected String TAG = MainListActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_list); if(isNetworkAvailable()) { GetPostsTask getPostsTask = new GetPostsTask(); getPostsTask.execute(); }else { Toast.makeText(this,"No Network", Toast.LENGTH_SHORT).show(); } } private boolean isNetworkAvailable(){ ConnectivityManager manager =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager.getActiveNetworkInfo(); boolean isAvailable = false ; if (networkInfo != null && networkInfo.isConnected()){ isAvailable = true ; } return isAvailable; } private class GetPostsTask extends AsyncTask<Object, Void, String>{ protected String doInBackground(Object...arg){ JSONObject jsonResponse ; try{ URL url = new URL(http://www.example.com/recent_summary/?count=20 ); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.connect(); int responseCode = connection.getResponseCode(); Log.i("Response Code", ""+responseCode); InputStream inputStream = connection.getInputStream(); Reader reader = new InputStreamReader(inputStream); BufferedReader r = new BufferedReader(reader); StringBuffer buffer = new StringBuffer(); String line ; while((line=r.readLine()) != null){ buffer.append(line + "\n"); } Log.i("ResponseData" , ""+buffer); jsonResponse = new JSONObject(buffer.toString()); JSONArray jsonPosts = jsonResponse.getJSONArray("posts") ; for(int i =0 ; i<jsonPosts.length() ; i++){ JSONObject jsonPost = jsonPosts.getJSONObject(i); String title = jsonPost.getString("title"); Log.i("Post"+i, title); } }catch (MalformedURLException ex){ Log.i(TAG , "Error Found ", ex); }catch(IOException ex){ Log.i(TAG , "Error Found ", ex); }catch (Exception ex){ Log.i(TAG , "Error Found ", ex); } return null ; } } } 
1
  • 1
    share the json what you get in response? Commented Feb 28, 2015 at 13:48

1 Answer 1

1

Seems your url not conveying the required item count to the server correctly. 10 should be the default item count that server is configured to return if you haven't provide an item count. Check your url again to see whether it formed correctly.

Your provided url

http://www.example.com/recent_summary/?count=20 

has a '/' before the '?' character. May be that cause the problem. So try the url without '/' character like this

http://www.example.com/recent_summary?count=20 
Sign up to request clarification or add additional context in comments.

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.