-1

I am trying to get the data from URL and convert it into String Array, but when I try to show it on TextView its show nothing. The link and itemId that I use is already right.

There's no Error warning on logcat.

I can't figure it out why. Is there anyone can't help me?

Here's my json data

[{"image":"http://cdn-data.apps.com/category_item/05d92b217f916c9b9d87ab9121662d87.jpg"},
{"image":"http://cdn-data.apps.com/category_item/0424ef5a980255ff989fe5b20eaa5dcd.jpg"},
{"image":"http://cdn-data.apps.com/category_item/02b5bce4a9ca8fa3e53ceea2c7e273ff.jpg"},
{"image":"http://cdn-data.apps.com/category_item/dd15419113c091c93eafb3695eb65153.jpg"},
{"image":"http://cdn-data.apps.com/category_item/1ddfd2d7a489678e3c66e7f012ceb951.jpg"}]

Here's my Java code

    if(in.getStringExtra("TAG_IMAGE_COUNT").equals("0")) {
        ViewPager imagePager = (ViewPager) findViewById(R.id.viewPagerGallery);
        imagePager.setVisibility(View.GONE);
        CirclePageIndicator imageIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
        imageIndicator.setVisibility(View.GONE);
    }
    else {
        ViewPager imagePager = (ViewPager) findViewById(R.id.viewPagerGallery);
        imagePager.setVisibility(View.VISIBLE);
        CirclePageIndicator imageIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
        imageIndicator.setVisibility(View.VISIBLE);

        try {

            DefaultHttpClient defaultClient = new DefaultHttpClient();
            HttpGet httpGetRequest = new HttpGet("http://api.apps.com/category_item/get_image/" + itemId);
            HttpResponse httpResponse = defaultClient.execute(httpGetRequest);

            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),"UTF-8"));

            String json = reader.readLine();

            //JSONObject jsonObject = new JSONObject(json);
            JSONArray arr = new JSONArray(httpResponse);
            List<String> list = new ArrayList<String>();
            for(int i = 0; i < arr.length(); i++){
                list.add(arr.getJSONObject(i).getString("image"));
            }
            String[] stringArr = list.toArray(new String[list.size()]);

            TextView array = (TextView)findViewById(R.id.testarray);

            array.setText(Arrays.toString(stringArr));
            Log.d("", json);

            //Toast.makeText(getApplicationContext(), json, Toast.LENGTH_SHORT).show();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

I try to show it on testarray but its show nothing.

4
  • Where do you place this code? You might be trying to update the view from a background thread instead of the UI thread. Commented Jul 29, 2014 at 7:16
  • I put on my second activity. When I click a button its called this activity and show the image. Commented Jul 29, 2014 at 7:20
  • Using your debugger, what is the value of arr after the line JSONArray arr = new JSONArray(httpResponse); Commented Jul 29, 2014 at 7:24
  • Your list to array and array to string code working perfectly so problem on your response. Commented Jul 29, 2014 at 7:27

1 Answer 1

0

First of all your have to parse a String not Httpresponse:

here you do:

 HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
 JSONArray arr = new JSONArray(httpResponse);

Change a little like:

 HttpResponse httpResponse = defaultClient.execute(httpGetRequest);

        BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),"UTF-8"));
        String returnValues = "";

        while((returnValues = reader .readLine()) != null){
          JSONArray arr = new JSONArray(returnValues);

        //Define this list globally ,so you can use it any where of this class after adding data
        List<String> list = new ArrayList<String>();

        for(int i = 0; i < arr.length(); i++){
            list.add(arr.getJSONObject(i).getString("image"));
        }
      }

And finally set the list data as you want in your textView out of the background thread. you should not use any UI thread views (e.g button, textview, edittext ..) in background thread.

EDIT:

private class GetImages extends AsyncTask<String, Integer, ArrayList<String>>{
   ArrayList<String> list = new ArrayList<String>();
    @Override
    protected ArrayList<String> doInBackground(String... params) {
        try {

        HttpClient defaultClient = new DefaultHttpClient();
        HttpGet httpGetRequest = new HttpGet("http://api.apps.com/category_item/get_image/" + itemId);
        HttpResponse httpResponse = defaultClient.execute(httpGetRequest);

        BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        String returnValues = "";

        while((returnValues = reader .readLine()) != null){
          JSONArray arr = new JSONArray(returnValues);

        //Define this list globally ,so you can use it any where of this class after adding data           
        for(int i = 0; i < arr.length(); i++){
            list.add(arr.getJSONObject(i).getString("image"));
        }
      }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        return list;
  }

    @Override
    protected void onPostExecute(ArrayList<String> result) {
        //use this result and put in your textView
    }
}
17
  • In this example, returnValues will just be a line from the response, not the entire array. Commented Jul 29, 2014 at 7:28
  • its ok..but as i know we have do network operation in background thread..ok let me edit the full answer for you.. Commented Jul 29, 2014 at 7:30
  • @RanjitPati before I call try, I check it first, if there're an images it call this function. So I think I can't use this one . Check My code, I edit it Commented Jul 29, 2014 at 7:47
  • @Matthew do all network operations in background thread/asynctask. otherwise it will throw NetworkOnMainthreadException.. Commented Jul 29, 2014 at 8:22
  • @RanjitPati so I need to create a new class that only contain asynctask and then call it on other activity? Sorry if i ask to much, I am new at this Commented Jul 29, 2014 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.