2017-12-27 41 views
1

私はのコードにを使用したいと思います。要求の種類は以下の回答に使用してください。理解できません。 URL ::: http://localhost/api/product/read.phpVolley JSONレスポンス

{ 
     "data":[ 
     { 
     "category_id":"1", 
     "category_name":"Today's Recipe", 
     "recipes":[ 
      { 
      "id":"1", 
      "recipe_name":"Peanut, and Chilli Noodles", 
      "ingredients":"Serves: 4 \r\n\r\n250g (9 oz) fresh Chinese , 
      "prepration":"Prep:15min Cook:10min Ready in:25min \r\n\r\nCook 
      noodles in a large pot of boiling water until done. have 
      chilli paste, use minced red chilli to taste." 
    } 
    ] 
    }, 
+0

を助けこれはあなたがJsonRequestを使用する必要がありますJsonObjectRequest https://developer.android.com/training/volley/request.html – joao86

+0

を使用する必要がありますので、JSONオブジェクトのように見えます。このブログの私の答えを参照してください: https://stackoverflow.com/questions/47936955/i-am-unable-to-make-a-network-call-using-volley-i-have-tried-many-solutions - ブタ/ 47937120#47937120 –

+0

私はエラー表示次jsonobject要求を使用し::: com.android.volley.ParseError:org.json.JSONException:java.lang.String型の値の接続がjSONObject –

答えて

0

これを試してみてください:

RequestQueue requestQueue = Volley.newRequestQueue(context); 
    final String url = "url here"; 

    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null, 
      new Response.Listener<JSONObject>() 
      { 
       @Override 
       public void onResponse(JSONObject response) 
       { 
       Log.e(" result",(String.valueOf(response))); 
       } 
      }, 
      new Response.ErrorListener() 
      { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      } 
    ); 
    requestQueue.add(getRequest); 

は、この情報がお役に立てば幸いです。

+0

私もそれを試していましたが、文字列リクエストがJsonオブジェクトに変換できません –

+0

これを見てくださいhttps://stackoverflow.com/questions/17992753/can-not-convert-json-string-to-object – nimi0112

0
StringRequest jsonObjectRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 

       if (response != null) { 
        try { 
JSONObject jsonObject = new JSONObject(response); 
         if (jsonObject.getString("status").equalsIgnoreCase("success")) { 
        JSONArray jsonArray = jsonObject.getJSONArray("data"); 
if(jsonArray!=null && jsonArray.length()>0){ 
    for(int i=0;i<jsonArray.length();i++){ 
JSONObject data_object=jsonArray.getJSONObject(i); 
JSONArray recepie = data_object.getJSONArray("recipes"); 

} 
//       
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       } 

     }); 
     MySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest); 
     jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
       APIConstants.API_TIMEOUT, //for 30 Seconds 
       DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
       DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
    } 

希望これは