这是我的Android代码。当我试图解析我的jsonArray时,它给了我一个错误,即字符串无法通过我的响应转换为Jsonarray是json数组:
public class Main2Activity extends AppCompatActivity {
EditText tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv1= (EditText) findViewById(R.id.textView);
DefaultOAuthConsumer defaultOAuthConsumer = new DefaultOAuthConsumer("my consumer key","my secret key");
//REQUEST URL
String url = "http://back.unaux.com/wp-json/wc/v2/products";
RequestQueue requestQueue;
requestQueue = Volley.newRequestQueue(this);
try {
url = defaultOAuthConsumer.sign(url);
Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
JsonArrayRequest req=new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Toast.makeText(Main2Activity.this, response.toString(), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Main2Activity.this,"Error"+error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(req);
}
}
这是我对json的回应
[
{
"id":685,
"name":"Caramel-min",
"slug":"caramel-min",
"permalink":"http:\/\/back.unaux.com\/product\/caramel-min\/",
"date_created":"2017-09-26T18:56:01",
"date_created_gmt":"2017-09-26T18:56:01",
"date_modified":"2017-09-27T12:27:56",
"date_modified_gmt":"2017-09-27T12:27:56",
"type":"variable",
"status":"publish",
"featured":false,
"catalog_visibility":"visible",
"description":"",
"short_description":"",
"sku":"",
"price":"350",
"regular_price":"350",
"sale_price":"",
"date_on_sale_from":null,
"date_on_sale_from_gmt":null,
"date_on_sale_to":null,
"date_on_sale_to_gmt":null,
"price_html":"<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">₹<\/span>350.00<\/span> – <span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">₹<\/span>500.00<\/span>",
"on_sale":false,
"purchasable":true,
"total_sales":2,
"virtual":false,
"downloadable":false,
"downloads":[
],
"download_limit":-1,
"download_expiry":-1,
"external_url":"",
"button_text":"",
"tax_status":"taxable",
"tax_class":"",
"manage_stock":false,
"stock_quantity":null,
"in_stock":true,
"backorders":"no",
"backorders_allowed":false,
"backordered":false,
"sold_individually":false,
"weight":"1.0",
"dimensions":{
"length":"",
"width":"",
"height":""
},
"shipping_required":true,
"shipping_taxable":true,
"shipping_class":"",
"shipping_class_id":0,
"reviews_allowed":true,
"average_rating":"0.00",
"rating_count":0,
"related_ids":[
],
"upsell_ids":[
],
"cross_sell_ids":[
],
"parent_id":0,
"purchase_note":"",
"categories":[
{
"id":43,
"name":"Chocolate",
"slug":"chocolate"
}
],
"tags":[
],
"images":[
{
"id":710,
"date_created":"2017-09-27T12:25:19",
"date_created_gmt":"2017-09-27T12:25:19",
"date_modified":"2017-09-27T12:25:19",
"date_modified_gmt":"2017-09-27T12:25:19",
"src":"http:\/\/back.unaux.com\/wp-content\/uploads\/2015\/08\/Caramel-min-1.jpg",
"name":"Caramel-min-1",
"alt":"",
"position":0
},
{
"id":710,
"date_created":"2017-09-27T12:25:19",
"date_created_gmt":"2017-09-27T12:25:19",
"date_modified":"2017-09-27T12:25:19",
"date_modified_gmt":"2017-09-27T12:25:19",
"src":"http:\/\/back.unaux.com\/wp-content\/uploads\/2015\/08\/Caramel-min-1.jpg",
"name":"Caramel-min-1",
"alt":"",
"position":1
}
],
"attributes":[
{
"id":0,
"name":"Kg",
"position":0,
"visible":true,
"variation":true,
"options":[
"1\/2",
"1.0",
"2.0",
"3.0",
"4.0",
"5.0"
]
}
],
"default_attributes":[
],
"variations":[
705,
704
],
"grouped_products":[
],
"menu_order":0,
"meta_data":[
{
"id":1369,
"key":"_vc_post_settings",
"value":{
"vc_grid_id":[
]
}
},
{
"id":1404,
"key":"slide_template",
"value":"default"
}
],
"_links":{
"self":[
{
"href":"http:\/\/back.unaux.com\/wp-json\/wc\/v2\/products\/685"
}
],
"collection":[
{
"href":"http:\/\/back.unaux.com\/wp-json\/wc\/v2\/products"
}
]
}
}
]
答案 0 :(得分:-1)
你在array.Test中有数组:添加排球库
的OnCreate(){
sendRequest将(); }
private void sendRequest() { try { StringRequest stringRequest = new StringRequest(test_urls, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.e("respnose", "" + response); parseJSON(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { progressDialog.dismiss(); Toast.makeText(Page_details_bkp.this, error.getMessage(), Toast.LENGTH_LONG).show(); } }); RequestQueue requestQueue = Volley.newRequestQueue(Page_details_bkp.this); requestQueue.add(stringRequest); } catch (Exception e) { progressDialog.dismiss(); Log.e("sendreq:", e.toString()); } } public void parseJSON(String json) { JSONObject jsonObject = null; try { jsonObject = new JSONObject(json); users = jsonObject.getJSONArray(JSON_ARRAY); try { userID = jsonObject.getString("api_status"); //ids = new String[jsonArray.length()]; if (userID.equals("success")) { ids = new String[users.length()]; names = new String[users.length()]; emails = new String[users.length()]; sub_images1 = new String[users.length()][3]; sub_ids = new String[users.length()][3]; // images = new String[users.length()]; //log.e("length :", users.length() + ""); for (int i = 0; i < users.length(); i++) { JSONObject jo = users.getJSONObject(i); ids[i] = jo.getString(KEY_ID); names[i] = jo.getString(KEY_NAME); emails[i] = jo.getString(KEY_EMAIL); try { JSONObject locArrObj = users.getJSONObject(i); // cotains one "out" array JSONArray conferenceLocArr = locArrObj.getJSONArray("posts"); if (conferenceLocArr.length() > 0) { // this array has two objects and each object has array JSONObject o = null; for (int ii = 0; ii < conferenceLocArr.length(); ii++) { o = conferenceLocArr.getJSONObject(ii); // it has one array try { // if (!o.getString("image").equals("")) { sub_images1[i][ii] = o.getString("image"); sub_ids[i][ii] = o.getString("id"); //log.e("Main response:", ids[i] + "," + ids[i] + "," + names[i] + "," + emails[i]); //} } catch (JSONException e) { e.printStackTrace(); //log.e("sub response:", e.toString()); } // sub_images1[i] = o.getString("image"); // do your work with Stage 1 and guests // and for second object for Stage 2 and guests. } Categories movie = new Categories(ids[i], names[i], emails[i], "2015", sub_images1[i][0],
sub_ids [i] [0],sub_images1 [i] [1],sub_ids [i] [1],sub_images1 [i] [2], sub_ids [I] [2]); movieList.add(电影); } } catch(JSONException e){ //log.e("response:",e.toString()); e.printStackTrace(); } }
} else { } } catch (JSONException e) { //log.e("response:", e.toString()); e.printStackTrace(); } finally { prepareMovieData(); } } catch (JSONException e) { //log.e("parse:", e.toString()); } }