Java Android:将列表数组转换为字符串数组不显示任何内容

Java Android:将列表数组转换为字符串数组不显示任何内容,java,android,arrays,json,Java,Android,Arrays,Json,我试图从URL获取数据并将其转换为字符串数组,但当我试图在TextView上显示它时,它什么也不显示。我使用的链接和itemId已经正确了 logcat上没有错误警告 我不明白为什么。 有人帮不了我吗 这是我的json数据 [{"image":"http://cdn-data.apps.com/category_item/05d92b217f916c9b9d87ab9121662d87.jpg"}, {"image":"http://cdn-data.apps.com/category_item

我试图从URL获取数据并将其转换为字符串数组,但当我试图在
TextView
上显示它时,它什么也不显示。我使用的链接和
itemId
已经正确了

logcat上没有错误警告

我不明白为什么。 有人帮不了我吗

这是我的json数据

[{"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"}]
这是我的Java代码

    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();
        }
    }
if(in.getStringExtra(“标记图像计数”)等于(“0”){
ViewPager imagePager=(ViewPager)findViewById(R.id.viewPagerGallery);
imagePager.setVisibility(View.GONE);
CirclePageIndicator imageIndicator=(CirclePageIndicator)findViewById(R.id.indicator);
imageIndicator.setVisibility(视图已消失);
}
否则{
ViewPager imagePager=(ViewPager)findViewById(R.id.viewPagerGallery);
imagePager.setVisibility(视图.可见);
CirclePageIndicator imageIndicator=(CirclePageIndicator)findViewById(R.id.indicator);
imageIndicator.setVisibility(视图.可见);
试一试{
DefaultHttpClient defaultClient=新的DefaultHttpClient();
HttpGet httpGetRequest=新的HttpGet(“http://api.apps.com/category_item/get_image/“+项目ID);
HttpResponse HttpResponse=defaultClient.execute(httpGetRequest);
BufferedReader=新的BufferedReader(新的InputStreamReader(httpResponse.getEntity().getContent(),“UTF-8”);
字符串json=reader.readLine();
//JSONObject JSONObject=新的JSONObject(json);
JSONArray arr=新JSONArray(httpResponse);
列表=新的ArrayList();
对于(int i=0;i

我试图在
testarray
上显示它,但它什么也不显示。

首先,您必须解析一个字符串,而不是Httpresponse:

给你:

 HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
 JSONArray arr = new JSONArray(httpResponse);
改变一下,比如:

 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"));
        }
      }
HttpResponse HttpResponse=defaultClient.execute(httpGetRequest);
BufferedReader=新的BufferedReader(新的InputStreamReader(httpResponse.getEntity().getContent(),“UTF-8”);
字符串returnValues=“”;
while((returnValues=reader.readLine())!=null){
JSONArray arr=新JSONArray(返回值);
//全局定义此列表,以便在添加数据后可以在此类的任何位置使用它
列表=新的ArrayList();
对于(int i=0;i
最后,在后台线程的textView中设置所需的列表数据。 您不应该在后台线程中使用任何UI线程视图(例如按钮、文本视图、编辑文本…)

编辑:

私有类GetImages扩展异步任务{
ArrayList=新建ArrayList();
@凌驾
受保护的ArrayList doInBackground(字符串…参数){
试一试{
HttpClient defaultClient=新的DefaultHttpClient();
HttpGet httpGetRequest=新的HttpGet(“http://api.apps.com/category_item/get_image/“+项目ID);
HttpResponse HttpResponse=defaultClient.execute(httpGetRequest);
BufferedReader=new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent());
字符串returnValues=“”;
while((returnValues=reader.readLine())!=null){
JSONArray arr=新JSONArray(返回值);
//全局定义此列表,以便在添加数据后可以在此类的任何位置使用它
对于(int i=0;i
您将此代码放在哪里?您可能试图从后台线程而不是UI线程更新视图。我开始了第二个活动。当我单击一个按钮时,它称为此活动并显示图像;你的列表到数组和数组到字符串的代码工作得很好,所以你的响应有问题。在这个例子中,returnValues将只是响应的一行,而不是整个数组。没问题。但我知道我们在后台线程中进行了网络操作。好的,让我为你编辑完整的答案。谢谢你的回答。我试试看:D@RanjitPati在调用try之前,我首先检查它,如果有图像,它调用这个函数。所以我想我不能用这个。检查我的代码,我编辑it@Matthew在后台线程/异步任务中执行所有网络操作。否则,它将抛出NetworkOnMainthreadException。。
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
    }
}