2016-07-26 14 views
0

Nun lange Geschichte kurz ich dieser Art von Problem konfrontiert, Volley Anfrage an meine Server zu senden, Im Versuch, das mit json Array antwortet:Keine Antwort von jsonArray Anfrage

{ 
    "images": [{ 
     "product_serial_num": "1", 
     "product_title": "Abbadon", 
     "product_img": "http://1.2.3.4/android/uploads/1.jpg", 
     "product_price": "750", 
     "product_description": "The destroyer" 
    }] 
} 

aber egal was ich versuchte mich noch erhalten Volley Antwortfehler, mein Volley Anfrage: es funktioniert, nur die Array-Chaos Dinge für mich

requestQueue = Volley.newRequestQueue(getActivity()); 
    //JsonArrayRequest of volley 
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(MY_URL , 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        //parseData to parse the json response 
        parseData(response); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

        //If an error occurs that means end of the list has reached 
        Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show(); 
       } 
      }); 

    requestQueue.add(jsonArrayRequest); 

die seltsame Sache, dass mit Volley String Anfrage.

EDIT:

wie auch immer diese Weise kann ich meine Array tun bekommen Futter mit:

private void getData() { 
    //Adding the method to the queue by calling the method getDataFromServer 
    requestQueue.add(getDataFromServer(requestCount)); 
    //Incrementing the request counter 
    requestCount++; 
} 

//Request to get json from server we are passing an integer here 
//This integer will used to specify the page number for the request ?page = requestcount 
//This method would return a JsonArrayRequest that will be added to the request queue 
private JsonArrayRequest getDataFromServer(int requestCount) { 

    //JsonArrayRequest of volley 
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL_INDEX + String.valueOf(requestCount), 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        //Calling method parseData to parse the json response 
        parseData(response); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

        //If an error occurs that means end of the list has reached 
        Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show(); 
       } 
      }); 

    //Returning the request 
    return jsonArrayRequest; 
} 

Thank you!

+0

Antwort Json ist JSONObject nicht JSONArray. Verwenden 'JsonObjectRequest' von Volley statt' JsonArrayRequest' –

+0

Objekt und String beide arbeiten, tut nur Array – 2Stoned

+0

Da Antworttyp JSONObject –

Antwort

2

Verwenden Sie diesen Code, Ändern JsonArrayRequest zu JsonObjectRequest

JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(Request.Method.GET, MY_URL, null, 
     new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       //parseData to parse the json response 
       parseData(response); 
      } 
     }, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

       //If an error occurs that means end of the list has reached 
       Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

Array Antwort

"images": [{ 
    "product_serial_num": "1", 
    "product_title": "Abbadon", 
    "product_img": "http://1.2.3.4/android/uploads/1.jpg", 
    "product_price": "750", 
    "product_description": "The destroyer" 
}] 

Objekt Antwort

{ 
"images": [{ 
    "product_serial_num": "1", 
    "product_title": "Abbadon", 
    "product_img": "http://1.2.3.4/android/uploads/1.jpg", 
    "product_price": "750", 
    "product_description": "The destroyer" 
}] 
} 
+0

es macht den Job, aber warum kann ich Array Req nicht verwenden? – 2Stoned

+0

Ihre Antwort ist Objekt kein Array – vinoth12594

+0

Sek. Zeigen Sie verschiedene Snippet, mit ArrayReq was ist der Unterschied? – 2Stoned