2013-08-02 7 views
6

मैं वर्तमान में GETRequest का उपयोग बाकी जीईटी अनुरोध जारी करने के लिए कर रहा हूं। PUT अनुरोधों के लिए उपयोग करने के बारे में स्पष्ट नहीं है, जहां मुझे अद्यतन करने के लिए एक संपूर्ण JSON ऑब्जेक्ट को भेजने की आवश्यकता है। अनुरोध वस्तु PUT स्वीकार करेगी लेकिन मुझे यकीन नहीं है कि जेएसओएन ऑब्जेक्ट की अपेक्षा की जाती है।Android वॉली में PUT अनुरोध निष्पादित करने के लिए कैसे?

यहाँ मेरी json रखा जा करने के लिए है:

var xhr = new XMLHttpRequest(); 
    xhr.open('PUT', 'http://my.apiary.io/v1/records/{myid}.json'); 

    xhr.send("{\n \"isEditable\": false,\n \"isClosed\": true,\n  \"isAvail\": true\n}"); 

मैं कहाँ JSON डाल करने के लिए पता नहीं है:

{ 
    prop1 : true, 
    prop2 : false, 
    prop4 : true 
} 

यहाँ कैसे अपने उदाहरण के लिए apiary.io में प्रस्तुत की है।

धन्यवाद

public class GsonRequest<T> extends Request<T> { 

private final Gson gson ; 
private final Class<T> clazz; 
private final Map<String, String> headers; 
private final Listener<T> listener; 

public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> headers, 
     Listener<T> listener, ErrorListener errorListener) { 
    super(method, url, errorListener); 

    GsonBuilder gsonBuilder = new GsonBuilder(); 
    gsonBuilder.registerTypeAdapter(Timestamp.class, new TimestampDeserializer()); 
    this.gson = gsonBuilder.create(); 
    this.clazz = clazz; 
    this.headers = headers; 
    this.listener = listener; 
} 

@Override 
public Map<String, String> getHeaders() throws AuthFailureError { 
    return headers != null ? headers : super.getHeaders(); 
} 

@Override 
protected void deliverResponse(T response) { 
    listener.onResponse(response); 
} 

@Override 
protected Response<T> parseNetworkResponse(NetworkResponse response) { 
    try { 
     String json = new String(
       response.data, HttpHeaderParser.parseCharset(response.headers)); 
     return Response.success(
       gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response)); 
    } catch (UnsupportedEncodingException e) { 
     return Response.error(new ParseError(e)); 
    } catch (JsonSyntaxException e) { 
     return Response.error(new ParseError(e)); 
    } 
    } 
} 

और यहाँ अनुरोध अंदर आधार getBody तरीके हैं। ऐसा लगता है कि विधि.PUT पर पैरामीटर को संभालना प्रतीत होता है, लेकिन क्या होगा यदि यह एक JSON स्ट्रिंग है जिसे शरीर में भेजा जाना चाहिए?

/** 
* Returns the raw POST or PUT body to be sent. 
* 
* @throws AuthFailureError in the event of auth failure 
*/ 
public byte[] getBody() throws AuthFailureError { 
    Map<String, String> params = getParams(); 
    if (params != null && params.size() > 0) { 
     return encodeParameters(params, getParamsEncoding()); 
    } 
    return null; 
} 

/** 
* Converts <code>params</code> into an application/x-www-form-urlencoded encoded string. 
*/ 
private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) { 
    StringBuilder encodedParams = new StringBuilder(); 
    try { 
     for (Map.Entry<String, String> entry : params.entrySet()) { 
      encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding)); 
      encodedParams.append('='); 
      encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding)); 
      encodedParams.append('&'); 
     } 
     return encodedParams.toString().getBytes(paramsEncoding); 
    } catch (UnsupportedEncodingException uee) { 
     throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee); 
    } 
} 

अनुशंसित समाधान:

 // add a Json body. 
    public String jsonBody; 

    /** 
    * Returns the raw POST or PUT body to be sent. 
    * 
    * @throws AuthFailureError in the event of auth failure 
    */ 



    public byte[] getBody() throws AuthFailureError { 

    if ((getMethod() == Method.PUT) && (jsonBody != null)) 
    { 
     return jsonBody.getBytes(); // Encoding required????? 

    } 
    else 
    { 
     return super.getBody(); 
    } 

} 

उत्तर

5

सार आधार वर्ग Request एक निर्माता है जो एक Request.Method पहले पैरामीटर के रूप लेता है। volley.toolbox में सभी अनुरोध कार्यान्वयन भी इस तरह के एक निर्माता हैं।

मुझे यकीन है कि जहां GsonRequest से आ रहा है नहीं कर रहा हूँ लेकिन अगर यह एक निर्माता है जो एक Method लेता नहीं है, तो आप एक अपने आप को जोड़ सकते हैं।

संपादित करें: आप कस्टम अनुरोध निकाय और एमआईएम प्रकार क्रमशः क्रमशः getBody और getBodyContentType ओवरराइड कर सकते हैं।

+0

मैंने अपना प्रश्न GsonRequest के साथ अपडेट किया है। मुझे नहीं पता कि इस अनुरोध में JSON स्ट्रिंग को कहां रखा जाए। – TestBest

+4

ऐसा लगता है कि यह डेटा जमा करने का समर्थन नहीं करता है। आपको धारावाहिक ऑब्जेक्ट और उचित एमआईएम प्रकार (उदाहरण के लिए, 'एप्लिकेशन/जेसन') – Delyan

+0

वापस करने के लिए' getBody' और 'getBodyContentType' को ओवरराइड करना होगा, मैंने पोस्ट किया है कि जावास्क्रिप्ट apiary.io – TestBest

संबंधित मुद्दे