gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:00 25 4
gpt4 key购买 nike

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

┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐
│■■■■■■││ ││ ││ ││ │
│■■■■■■││ ││ ││ ││ │
└──────┘└──────┘└──────┘└──────┘└──────┘
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

在我提问之前,我需要多学点英语......


我希望我的应用程序图像按钮(或 GridView )上有这些 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_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout1"
android:layout_alignParentTop="true"
>

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

</LinearLayout>


<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout2">

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get"
/>

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

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout5" >


<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IP:" />

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
/>
</LinearLayout>

<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:visibility="gone" >

<requestFocus />
</EditText>

<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout4"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >


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

但是我试过这段代码,只有第一张图片显示,另一张没有出现帮帮我!!

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

最佳答案

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

     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);

关于android - 带有onclick的android图像按钮上的JSON数组响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34261213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com