带有onclick的android imagebuttons上的JSON数组响应

Posted

技术标签:

【中文标题】带有onclick的android imagebuttons上的JSON数组响应【英文标题】:JSon array response on android imagebuttons with onclick 【发布时间】:2016-03-19 14:09:54 【问题描述】:

对不起,我的问题只是出现了一行 在我的项目中 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 数组响应 我从 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<String> imageUrlList = new ArrayList<String>();
                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代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<LinearLayout 
    android:id="@+id/linearLayout"
    android:layout_
    android:layout_
    android:layout_above="@+id/linearLayout1"
    android:layout_alignParentTop="true"
    >

    <ImageButton
        android:id="@+id/picButton"
        android:layout_
        android:layout_
        android:padding="0sp"
        android:scaleType="centerCrop"
        android:background="@drawable/ic_launcher1" />

</LinearLayout>


<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_
    android:layout_ 
    android:layout_above="@+id/linearLayout2">

    <Button
        android:id="@+id/button3"
        android:layout_
        android:layout_
        android:text="Get"
       />

    <TextView
        android:id="@+id/textView4"
        android:layout_
        android:layout_
        android:layout_weight="0.66"
        android:text="c2db6c9be8e5407c8a226ba8a0851368"
        android:visibility="gone" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_
    android:layout_
    android:layout_above="@+id/linearLayout5" >


    <EditText
        android:id="@+id/editText2"
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:background="@android:color/white"
        android:ems="10"
        android:paddingTop="33px"
        android:inputType="textMultiLine"
        android:hint="comment" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_
    android:layout_
    android:visibility="gone">

    <TextView
        android:id="@+id/textView1"
        android:layout_
        android:layout_
        android:text="IP:" />

    <EditText
        android:id="@+id/editText1"
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:text=""
         />
</LinearLayout>

<EditText
    android:id="@+id/editText3"
    android:layout_
    android:layout_
    android:ems="10"
    android:visibility="gone" >

    <requestFocus />
</EditText>

<LinearLayout
    android:id="@+id/linearLayout4"
    android:layout_
    android:layout_
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:orientation="vertical"
    android:weightSum="10"
    android:paddingBottom="59px"
    android:paddingTop="10dp">


    <Button
        android:id="@+id/button2"
        android:layout_
        android:layout_
        android:layout_gravity="center"
        android:layout_weight="3"
        android:text="POST"
        android:paddingLeft="100px"
        android:paddingRight="100px"
        android:textStyle="bold"
        android:textAlignment="center"
        android:textSize="60px"
        android:textColor="@android:color/white"
        />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout5"
    android:layout_
    android:layout_
    android:layout_above="@+id/linearLayout4"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal" >


    <ImageButton
        android:id="@+id/resultButton1"
        android:layout_
        android:layout_
        android:src="@drawable/ic_launcher1" />
    <ImageButton 
        android:id="@+id/resultButton2"
        android:layout_
        android:layout_
        android:src="@drawable/ic_launcher1"/>
    <ImageButton
        android:id="@+id/resultButton3"
        android:layout_
        android:layout_
        android:src="@drawable/ic_launcher1" />
    <ImageButton
        android:id="@+id/resultButton4"
        android:layout_
        android:layout_
        android:src="@drawable/ic_launcher1" />
    <ImageButton
        android:id="@+id/resultButton5"
        android:layout_
        android:layout_
        android:src="@drawable/ic_launcher1" />
</LinearLayout>

但我尝试使用此代码,只显示第一张图片,另一张没有出现 帮帮我!!

仅收到一份数据表(一行)即为成功。我不知道如何接收总行数据....请帮助我。请救救我的命

【问题讨论】:

因为你在imageUrlList中只添加了一个image_url。 您只将image_url 添加到列表中 如果你想在 listview 中添加所有 image_url 那么你必须这样做。 imageUrlList.add(image_url); imageUrlList.add(image_url1);imageUrlList.add(image_url2);imageUrlList.add(image_url3); 谢谢你的回复,但这不是我想要的 首先你告诉你得到的不是所有图片的url。如果你得到所有的 url,那么你没有在图像视图中正确设置。 【参考方案1】:

因为您只在列表中添加了一个 image_url。

如果您想在列表中添加所有 url,您必须执行以下操作,

imageUrlList.add(image_url);
imageUrlList.add(image_url1);
imageUrlList.add(image_url2);
imageUrlList.add(image_url3); 
imageUrlList.add(image_url4); 
imageUrlList.add(image_url5);
imageUrlList.add(image_url6);

但不是这个,

更好的方法是创建所有图片的JSONArray

【讨论】:

感谢您的评论,很抱歉我的问题是错误的,我的问题只是一个 imageurl 和 url1,2,3,4,5 出现我会纠正我的问题【参考方案2】:

试试这个代码它的工作..

     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<String> imageUrlList = new ArrayList<String>();
                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);
                    imageUrlList.add(image_url1);
                    imageUrlList.add(image_url2);
                    imageUrlList.add(image_url3);
                    imageUrlList.add(image_url4);
                    imageUrlList.add(image_url5);
                    imageUrlList.add(image_url6);  

【讨论】:

感谢您的评论,但请再次阅读我编辑的问题

以上是关于带有onclick的android imagebuttons上的JSON数组响应的主要内容,如果未能解决你的问题,请参考以下文章

带有onClick项目的Android ListView

带有onclick的android imagebuttons上的JSON数组响应

带有 CursorAdapter 的 Android ListView 项目 onClick 仅在一些点击后工作

Android:通过控制器添加带有 ids/onclick 的新布局元素

xml 透明背景。字体:https://stackoverflow.com/questions/3402787/how-to-have-a-transparent-imagebutton-androi

带有旋转图标的 Android 材质按钮