2017-12-27 14 views
1

내가 사용하려는 발리 내 코드에 요청 유형을 사용해야합니다. 아래에서 내 응답에 사용해야합니다. 이해할 수 없습니다. URL ::: http://localhost/api/product/read.php발리 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

을하는 데 도움이 JsonObjectRequest https://developer.android.com/training/volley/request.html 당신이 JsonRequest를 사용해야합니다 – joao86

+0

. 이 블로그의 내 대답을 참조하십시오. https://stackoverflow.com/questions/47936955/i-am-unable-to-make-a-network-call-using-volley-i-have-tried-many-solutions -하지만/47937120 # 47937120 –

+0

내가 에러 표시 ::: com.android.volley.ParseError 다음 된 JSONObject 요청을 사용 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)); 
    } 

희망이, 이것은 JSON 개체로 나타납니다