我只获得第一个id JSON对象数据,这是我的Interface类:
@POST("webservices.php?view=get_products")
Call<MultipleProductList> sendMultipleProducts();
和我的响应类我通过在线json解析网站创建了我的响应类 http://www.jsonschema2pojo.org/我仅获得第一个json对象数据,请尝试解决此问题:
{
"success": "true",
"data": [
{
"ID": "27",
"post_title": "Beanie",
"post_type": "product",
"post_content": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"post_status": "publish",
"post_name": "beanie",
"image": "http://localhost/mobileappstore/wp-content/uploads/2018/06/beanie.jpg",
"average_rating": "0",
"sku": "",
"regular_price": "20",
"sale_price": "18",
"total_sales": "0",
"stock_status": "instock",
"price": "18"
},
{
"ID": "28",
"post_title": "Belt",
"post_type": "product",
"post_content": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"post_status": "publish",
"post_name": "belt",
"image": "http://localhost/mobileappstore/wp-content/uploads/2018/06/belt.jpg",
"average_rating": "0",
"sku": "",
"regular_price": "65",
"sale_price": "55",
"total_sales": "0",
"stock_status": "instock",
"price": "55"
},
{
"ID": "29",
"post_title": "Cap",
"post_type": "product",
"post_content": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"post_status": "publish",
"post_name": "cap",
"image": "http://localhost/mobileappstore/wp-content/uploads/2018/06/cap.jpg",
"average_rating": "0",
"sku": "",
"regular_price": "18",
"sale_price": "16",
"total_sales": "0",
"stock_status": "instock",
"price": "16"
},
{
"ID": "30",
"post_title": "Sunglasses",
"post_type": "product",
"post_content": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"post_status": "publish",
"post_name": "sunglasses",
"image": "http://localhost/mobileappstore/wp-content/uploads/2018/06/sunglasses.jpg",
"average_rating": "0",
"sku": "",
"regular_price": "90",
"sale_price": "",
"total_sales": "0",
"stock_status": "instock",
"price": "90"
},
{
"ID": "31",
"post_title": "Hoodie with Logo",
"post_type": "product",
"post_content": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"post_status": "publish",
"post_name": "hoodie-with-logo",
"image": "http://localhost/mobileappstore/wp-content/uploads/2018/06/hoodie-with-logo.jpg",
"average_rating": "0",
"sku": "",
"regular_price": "45",
"sale_price": "",
"total_sales": "0",
"stock_status": "instock",
"price": "45"
},
{
"ID": "32",
"post_title": "Hoodie with Pocket",
"post_type": "product",
"post_content": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"post_status": "publish",
"post_name": "hoodie-with-pocket",
"image": "http://localhost/mobileappstore/wp-content/uploads/2018/06/hoodie-with-pocket.jpg",
"average_rating": "0",
"sku": "",
"regular_price": "45",
"sale_price": "35",
"total_sales": "0",
"price": "35",
"stock_status": "instock"
},
{
"ID": "33",
"post_title": "Hoodie with Zipper",
"post_type": "product",
"post_content": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"post_status": "publish",
"post_name": "hoodie-with-zipper",
"image": "http://localhost/mobileappstore/wp-content/uploads/2018/06/hoodie-with-zipper.jpg",
"average_rating": "0",
"sku": "",
"regular_price": "45",
"sale_price": "",
"total_sales": "0",
"stock_status": "instock",
"price": "45"
},
{
"ID": "34",
"post_title": "Hoodie",
"post_type": "product",
"post_content": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"post_status": "publish",
"post_name": "hoodie",
"image": "http://localhost/mobileappstore/wp-content/uploads/2018/06/hoodie.jpg",
"average_rating": "0",
"sku": "",
"regular_price": "45",
"sale_price": "42",
"total_sales": "0",
"stock_status": "instock",
"price": "42"
},
{
"ID": "35",
"post_title": "Long Sleeve Tee",
"post_type": "product",
"post_content": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"post_status": "publish",
"post_name": "long-sleeve-tee",
"image": "http://localhost/mobileappstore/wp-content/uploads/2018/06/long-sleeve-tee.jpg",
"average_rating": "0",
"sku": "",
"regular_price": "25",
"sale_price": "",
"total_sales": "0",
"stock_status": "instock",
"price": "25"
},
{
"ID": "36",
"post_title": "Polo",
"post_type": "product",
"post_content": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"post_status": "publish",
"post_name": "polo",
"image": "http://localhost/mobileappstore/wp-content/uploads/2018/06/polo.jpg",
"average_rating": "0",
"sku": "",
"regular_price": "20",
"sale_price": "",
"total_sales": "0",
"stock_status": "instock",
"price": "20"
},
{
"ID": "37",
"post_title": "Tshirt",
"post_type": "product",
"post_content": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"post_status": "publish",
"post_name": "tshirt",
"image": "http://localhost/mobileappstore/wp-content/uploads/2018/06/tshirt.jpg",
"average_rating": "0",
"sku": "",
"regular_price": "18",
"sale_price": "",
"total_sales": "0",
"stock_status": "instock",
"price": "18"
},
{
"ID": "38",
"post_title": "Vneck Tshirt",
"post_type": "product",
"post_content": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"post_status": "publish",
"post_name": "vneck-tee",
"image": "http://localhost/mobileappstore/wp-content/uploads/2018/06/vneck-tee.jpg",
"average_rating": "0",
"sku": "",
"regular_price": "18",
"sale_price": "",
"total_sales": "0",
"stock_status": "instock",
"price": "18"
}
],
"msg": "product list"
}
我得到了第一个id响应,我的改造课程是:
public void setData() {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(API_URL)
.build();
Apiinterface apiinterface = retrofit.create(Apiinterface.class);
Call<MultipleProductList> call = apiinterface.sendMultipleProducts();
call.enqueue(new Callback<MultipleProductList>() {
@Override
public void onResponse(Call<MultipleProductList> call, Response<MultipleProductList> response) {
response.body();
String success = response.body().getSuccess();
String message = response.body().getMsg();
Log.e("success", success);
Log.e("msg", message);
if (response.isSuccessful()) {
String responseData = response.body().toString();
Log.e("here", "onResponse: " + responseData);
MultipleProductList multipleProductList = response.body();
multipleProductList.getData();
List<DataProduct> arrayList = new ArrayList<>();
arrayList = multipleProductList.getData();
arrayList.size();
String id = arrayList.get(0).getID();
String post_type = arrayList.get(0).getPostType();
String regularPrice = arrayList.get(0).getRegularPrice();
String sale_price = arrayList.get(0).getSalePrice();
String post_content = arrayList.get(0).getPostContent();
String post_status = arrayList.get(0).getPostStatus();
String post_name = arrayList.get(0).getPostName();
String average_rating = arrayList.get(0).getAverageRating();
String sku = arrayList.get(0).getSku();
String post_title = arrayList.get(0).getPostTitle();
String total_sales = arrayList.get(0).getTotalSales();
String stock_status = arrayList.get(0).getStockStatus();
String price = arrayList.get(0).getPrice();
String img = arrayList.get(0).getImage();
Picasso.with(getApplicationContext()).load(img).fit().into(img_saree);
/* Picasso.with(context)
.load(img)
.into(img_saree);*/
Log.e("id", id);
Log.e("regularPrice", regularPrice);
Log.e("sale_price", sale_price);
Log.e("image_pic", img);
Log.e("post_content", post_content);
Log.e("post_status", post_status);
Log.e("post_name", post_name);
Log.e("average_rating", average_rating);
Log.e("sku", sku);
Log.e("post_title", post_title);
Log.e("total_sales", total_sales);
Log.e("stock_status", stock_status);
Log.e("price", price);
Log.e("post_type", post_type);
Gson gson = new Gson();
} else {
Log.i("debug", "onResponse: GA BERHASIL");
}
}
答案 0 :(得分:0)
使用截击尝试一下
private void JSON_HTTP_CALL(String url)
{
RequestQueue requestQueue= Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject=new JSONObject(response);
JSONArray jsonArray=jsonObject.getJSONArray("user_pro");
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject1=jsonArray.getJSONObject(i);
String name,email,gender,mobileno,city,country;
String base_url = getString(R.string.url);
String img_path = base_url+jsonObject1.getString("emp_image");
jsonObject1.getString("id");
name=jsonObject1.getString("name");
email=jsonObject1.getString("email");
Picasso.with(getApplicationContext()).load(img_path).into(User_pic);
User_name.setText(name);
User_email.setText(email);
}
}catch (JSONException e){e.printStackTrace();}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
int socketTimeout = 30000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
requestQueue.add(stringRequest);
}