当前位置:  开发笔记 > 编程语言 > 正文

带有onclick的android图像按钮上的JSon数组响应

如何解决《带有onclick的android图像按钮上的JSon数组响应》经验,为你挑选了0个好方法。

对不起,我的问题只是出现了一行而且在我的项目中image_url1,2,3,4,5是按意图对另一个活动进行操作,它运行良好已经无法上传图片,请看下面的图片

????????????????????????????????????????
?????????      ??      ??      ??      ?
?????????      ??      ??      ??      ?
????????????????????????????????????????
 only first url parsing works another didn't.... 
(first box parsed to image_url, when it clicks image_url1,2,3,4,5 appear)
i want all rows appear, not one row

在我提问之前,我需要更多地学习英语......


我希望这些json数组响应在我的应用程序图像按钮(或gridview)上我得到一个来自WAS的图像数组,每个行数据充当按钮json数组在这里

{
"total" : 2,
"row" : [
     {
      "id": "c3asfasfas35sd4a35as5d4a3",
      "image_name": "20150913151562135",
      "image_url": "http://myurl/imagelocation.jpg",
      "flag": null,
      "price": "1200000",
      "image_url1": "http://image_url1/imagelocation.jpg",
      "image_url2": "http://image_url2/imagelocation.jpg",
      "image_url3": "http://image_url3/imagelocation.jpg",
      "image_url4": "http://image_url4/imagelocation.jpg",
      "image_url5": "http://image_url5/imagelocation.jpg",
      "image_url6": "http://image_url6/imagelocation.jpg",
},
{
      "id": "c3asfasfas35sd4a35as5d4a3",
      "image_name": "20150913151562135",
      "image_url": "http://myurl/imagelocation.jpg",
      "flag": null,
      "price": "1200000",
      "image_url1": "http://image_url7/imagelocation.jpg",
      "image_url2": "http://image_url8/imagelocation.jpg",
      "image_url3": "http://image_url9/imagelocation.jpg",
      "image_url4": "http://image_url10/imagelocation.jpg",
      "image_url5": "http://image_url11/imagelocation.jpg",
      "image_url6": "http://image_url12/imagelocation.jpg",
}

这是我的班级获取数据

this is my activity about receive data

private class SearchThread implements Runnable {

    @Override
    public void run() {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(imageSearchUrl);
            MultipartEntity reqEntity = new MultipartEntity();
            StringBody part1 = new StringBody(imageId, Charset.forName("UTF-8"));
            reqEntity.addPart("imageId", part1);
            //pages =1 :0-2 2:5-6 3:6-8
            StringBody pages = new StringBody("1");
            reqEntity.addPart("pages", pages);

            post.setEntity(reqEntity);
            post.setHeader("enctype", "multipart/form-data;");
            HttpResponse response = client.execute(post);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {// success

                HttpEntity entity = response.getEntity();
                String resJson = EntityUtils.toString(entity);
                System.out.println("**** = " + resJson);
                jsonStr = resJson;
                JSONTokener jsonParser = new JSONTokener(resJson);
                JSONObject itemList = (JSONObject) jsonParser.nextValue();
                int total = itemList.getInt("total");
                int currentPage = itemList.getInt("currentPage");
                JSONArray jsonObjs = itemList.getJSONArray("rows");
                String demoUrl = "";
                String s = "";
                List imageUrlList = new ArrayList();
                for (int i = 0; i < jsonObjs.length(); i++) {
                    JSONObject jsonObj = jsonObjs.getJSONObject(i);
                    // String id = jsonObj.getInt("id");
                    String image_name = jsonObj.getString("image_name");
                    String image_url = jsonObj.getString("image_url");
                    String image_url1 = jsonObj.getString("image_url1");
                    String image_url2 = jsonObj.getString("image_url2");
                    String image_url3 = jsonObj.getString("image_url3");
                    String image_url4 = jsonObj.getString("image_url4");
                    String image_url5 = jsonObj.getString("image_url5");
                    String image_url6 = jsonObj.getString("image_url6");
                    String price = jsonObj.getString("price");
                    imageUrlList.add(image_url);

                    // s += " image_name = " + image_name + "image_url = " +
                    // image_url;

                    if (i == 0) {
                        urlStr = image_url1 +","+ image_url2 +","+ image_url3 +","+
                                 image_url4 +","+ image_url5 +","+ image_url6;
                        demoUrl = image_url;
                        System.out.println("########### " + image_url1 + " ---" + image_url2 + "---" + image_url3 + " ---" + image_url4 + "---" + image_url5 + " ---"
                                + image_url6 + "---");
                    }
                }
                String s1 = demoUrl.replaceAll("127.0.0.1", "url");

                mHandler.obtainMessage(0, s1).sendToTarget();
            } else {

            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("%%%%%%%%%%%5" + e.toString());
        }

    }

}

public String uploadImage(String url, String filepath) {
    File file = new File(filepath);

    if (!file.exists()) {
        return null;
    }

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    FileBody fileBody = new FileBody(file, "image/jpeg");
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("image", fileBody);

    post.setEntity(entity);

    try {
        HttpResponse response = client.execute(post);
        int statusCode = response.getStatusLine().getStatusCode();
        String result = EntityUtils.toString(response.getEntity(), "utf-8");

        if (statusCode == 201) {
            // upload success
            // do something
        }

        return result;
    } catch (Exception e) {
        System.out.println(e.toString());
    }

    return null;
}

private Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 0:
            try {
                URL url = new URL(msg.obj.toString());
                 et3.setText(msg.obj.toString());
                new Thread(new ImageRunnable()).start();
            } catch (Exception e) {
                System.out.println("^^^^^^^" + e.toString());
            }

            break;

        case 1:
            Toast.makeText(getApplication(), "failed", Toast.LENGTH_LONG).show();
            break;
        }
    }
};

private class ImageRunnable implements Runnable {
    @Override
    public void run() {
        // get the image by use url
        HttpClient hc = new DefaultHttpClient();
        HttpGet hg = new HttpGet(et3.getText().toString());
        final Bitmap bm;
        try {
            HttpResponse hr = hc.execute(hg);
            bm = BitmapFactory.decodeStream(hr.getEntity().getContent());
        } catch (Exception e) {
            mHandler2.obtainMessage(1).sendToTarget();
            return;
        }
        mHandler2.obtainMessage(0, bm).sendToTarget();
    }
};

private Handler mHandler2 = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 0:
            //ImageView iv2 = (ImageView) findViewById(R.id.imageView2);
        //  iv2.setImageBitmap((Bitmap) msg.obj);//
            imageButton1.setImageBitmap((Bitmap) msg.obj);
            Toast.makeText(getApplication(), "success", Toast.LENGTH_LONG).show();
            break;

        case 1:
            Toast.makeText(getApplication(), "failed", Toast.LENGTH_LONG).show();
            break;
        }
    }
};

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    switch (event.getAction()) {
    case KeyEvent.ACTION_UP: {

    }
    case KeyEvent.ACTION_DOWN: {


    }
    default:
        break;
    }
    return false;
}

我的xml代码




    






    

但我尝试这个代码,只有第一个图像显示,另一个没出现帮助我!

只收到一个数据表(一行)就成功了.我不知道如何接收总行数据....请帮帮我.请救命

推荐阅读
手机用户2502852037
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有