2015-06-16 6 views
9

में वॉली का उपयोग करके जेसन सरणी प्रतिक्रिया प्राप्त करने के लिए जेसन ऑब्जेक्ट डेटा पोस्ट करें मुझे JSONObject (Volley का उपयोग करके) एक वेब-सेवा पर पोस्ट करना होगा जो JSONArray प्रारूप में प्रतिक्रिया लौटा रहा है।एंड्रॉइड

यहां तक ​​कि मैंने अभी तक कोशिश की है।

final JSONObject requestJsonObject = new JSONObject(); 
requestJsonObject.put("username", userName); 
requestJsonObject.put("password", password); 

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, ServiceUrls.LOGIN_URL, requestJsonObject, loginResponseListener, loginErrorListener); 


private Listener<JSONObject> loginResponseListener = new Listener<JSONObject>() { 
    @Override 
    public void onResponse(JSONObject resposne) { 
     //other stuff goes here 
    } 
}; 

लेकिन मैं JSONException कह रही है कि JSONArrayJSONObject में परिवर्तित नहीं किया जा सकता है हो रही है। JSONArray प्रारूप में प्रतिक्रिया प्राप्त करने का कोई तरीका है? मेरी समस्या के लिए सबसे अच्छा संभव समाधान क्या है? यदि मैं JsonObjectRequest के बजाय StringRequest का उपयोग करता हूं तो मैं JSONObject कैसे भेज सकता हूं? कृपया मुझे मार्गदर्शन करें

+0

आप उपयोग कर सकते JsonArrayRequest –

+0

JsonArrayRequest JSONArray, जो इस मामले – anhtuannd

+0

आप डाल सकता आदमी – Shikhar

उत्तर

11

सहायक वर्ग नीचे मेरी समस्या तय किया है

public class CustomRequest extends JsonRequest<JSONArray> { 

protected static final String PROTOCOL_CHARSET = "utf-8"; 
/** 
* Creates a new request. 
* @param method the HTTP method to use 
* @param url URL to fetch the JSON from 
* @param requestBody A {@link String} to post with the request. Null is allowed and 
* indicates no parameters will be posted along with request. 
* @param listener Listener to receive the JSON response 
* @param errorListener Error listener, or null to ignore errors. 
*/ 
public CustomRequest(int method, String url, String requestBody, Listener<JSONArray> listener, ErrorListener errorListener) { 
    super(method, url, requestBody, listener, errorListener); 
} 

/** 
* Creates a new request. 
* @param url URL to fetch the JSON from 
* @param listener Listener to receive the JSON response 
* @param errorListener Error listener, or null to ignore errors. 
*/ 
public CustomRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener) { 
    super(Method.GET, url, null, listener, errorListener); 
} 

/** 
* Creates a new request. 
* @param method the HTTP method to use 
* @param url URL to fetch the JSON from 
* @param listener Listener to receive the JSON response 
* @param errorListener Error listener, or null to ignore errors. 
*/ 
public CustomRequest(int method, String url, Listener<JSONArray> listener, ErrorListener errorListener) { 
    super(method, url, null, listener, errorListener); 
} 

/** 
* Creates a new request. 
* @param method the HTTP method to use 
* @param url URL to fetch the JSON from 
* @param jsonRequest A {@link JSONArray} to post with the request. Null is allowed and 
* indicates no parameters will be posted along with request. 
* @param listener Listener to receive the JSON response 
* @param errorListener Error listener, or null to ignore errors. 
*/ 
public CustomRequest(int method, String url, JSONArray jsonRequest, Listener<JSONArray> listener, ErrorListener errorListener) { 
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener); 
} 

/** 
* Creates a new request. 
* @param method the HTTP method to use 
* @param url URL to fetch the JSON from 
* @param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and 
* indicates no parameters will be posted along with request. 
* @param listener Listener to receive the JSON response 
* @param errorListener Error listener, or null to ignore errors. 
*/ 
public CustomRequest(int method, String url, JSONObject jsonRequest, Listener<JSONArray> listener, ErrorListener errorListener) { 
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener); 
} 

/** 
* Constructor which defaults to <code>GET</code> if <code>jsonRequest</code> is 
* <code>null</code>, <code>POST</code> otherwise. 
* 
* @see #MyjsonPostRequest(int, String, JSONArray, Listener, ErrorListener) 
*/ 
public CustomRequest(String url, JSONArray jsonRequest, Listener<JSONArray> listener, ErrorListener errorListener) { 
    this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest, listener, errorListener); 
} 

/** 
* Constructor which defaults to <code>GET</code> if <code>jsonRequest</code> is 
* <code>null</code>, <code>POST</code> otherwise. 
* 
* @see #MyjsonPostRequest(int, String, JSONObject, Listener, ErrorListener) 
*/ 
public CustomRequest(String url, JSONObject jsonRequest, Listener<JSONArray> listener, ErrorListener errorListener) { 
    this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest, listener, errorListener); 
} 

@Override 
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) { 
    try { 
     String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); 
     return Response.success(new JSONArray(jsonString), HttpHeaderParser.parseCacheHeaders(response)); 
    } catch (UnsupportedEncodingException e) { 
     return Response.error(new ParseError(e)); 
    } catch (JSONException je) { 
     return Response.error(new ParseError(je)); 
    } 
} 

} 

यह कैसे उपयोग करने के लिए?

JSONObject requestJsonObject = new JSONObject(); 
    requestJsonObject.put("first_name", firstName); 
    requestJsonObject.put("last_name", lastName); 
    requestJsonObject.put("email_address", emailId); 
    requestJsonObject.put("password", password); 

    CustomRequest jsonObjReq = new CustomRequest(Method.POST, YOUR_URL, requestJsonObject, responseListener, errorListener); 
+0

धन्यवाद सर, कैसे इस वर्ग का उपयोग करने के लिए उपयुक्त नहीं है के रूप में इनपुट पैरामीटर की आवश्यकता है? –

+0

@mahdipishguy: मेरा उत्तर – Santhosh

+0

अपडेट किया गया मैंने आपके कोड का उपयोग किया और अनुरोध के दौरान जोड़ने के दौरान Queue jsonObjReq शून्य दिखाएं लेकिन requestjsonObject में इस प्रकार का मान ** "{" id ":" 1 "}" ** एक महत्वपूर्ण मूल्य जोड़ी है? ? और यदि यह सर्वर की तरफ से एक महत्वपूर्ण वैल्यू जोड़ी है, तो मैं इसे कैसे प्राप्त कर सकता हूं – techDigi

0

ऐसा लगता है कि आपको जेसन सरणी प्रतिक्रिया मिल रही है।

क्या आप इस तरह अपना कोड बदल सकते हैं।

private Listener<JsonArray> loginResponseListener = new Listener<JsonArray>() { 
     @Override 
     public void onResponse(JsonArray resposne) { 
     //other stuff goes here 
     } 
    }; 
-1

कोशिश यह एक

RequestQueue request = Volley.newRequestQueue(this); 
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url,new Response.Listener<JSONArray>() 
     { 

      @Override 
      public void onResponse(JSONArray response) 
      { 
       List<Contact> result = new ArrayList<Contact>(); 
       for (int i = 0; i < response.length(); i++) 
       { 
        try 
        { 
         result.add(convertContact(response 
           .getJSONObject(i))); 
        } 
        catch (JSONException e) 
        { 

        } 
       } 
       adpt.setItemList(result); 
       adpt.notifyDataSetChanged(); 
      } 
     }, new Response.ErrorListener() 
     { 

      @Override 
      public void onErrorResponse(VolleyError error) 
      { 
       // Handle error 
      } 
     }); 

request.add(jsonArrayRequest);