2015-11-17 10 views
9

मैं Retrofit 2रेट्रोफिट 2 - अशक्त प्रतिक्रिया शरीर

{ 
    "errorNumber":4, 
    "status":0, 
    "message":"G\u00f6nderilen de\u011ferler kontrol edilmeli", 
    "validate":[ 
     "Daha \u00f6nceden bu email ile kay\u0131t olunmu\u015f. L\u00fctfen giri\u015f yapmay\u0131 deneyiniz." 
    ] 
} 

साथ निम्नलिखित प्रतिक्रिया कन्वर्ट करने के लिए कोशिश कर रहा हूँ लेकिन मैं allways onResponse विधि में null प्रतिक्रिया हो रही है। तो मैंने response.errorBody.string() के साथ प्रतिक्रिया के त्रुटि निकाय को देखने की कोशिश की। त्रुटि निकाय में कच्ची प्रतिक्रिया के साथ बिल्कुल वही सामग्री होती है।

यहाँ मेरी सेवा विधि, Retrofit वस्तु और प्रतिक्रिया डेटा declerations है:

@FormUrlEncoded 
@POST("/Register") 
@Headers("Content-Type: application/x-www-form-urlencoded") 
Call<RegisterResponse> register(
     @Field("fullName") String fullName, 
     @Field("email")  String email, 
     @Field("password") String password); 

public class RegisterResponse { 
    public int status; 
    public String message; 
    public int errorNumber; 
    public List<String> validate; 
} 

OkHttpClient client = new OkHttpClient(); 
client.interceptors().add(new Interceptor() { 
    @Override 
    public Response intercept(Chain chain) throws IOException { 
     Response response = chain.proceed(chain.request()); 
     final String content = UtilityMethods.convertResponseToString(response); 
     Log.d(TAG, lastCalledMethodName + " - " + content); 
     return response.newBuilder().body(ResponseBody.create(response.body().contentType(), content)).build(); 
    } 
}); 
Retrofit retrofit = new Retrofit.Builder() 
     .baseUrl(BASE_URL) 
     .addConverterFactory(GsonConverterFactory.create()) 
     .client(client) 
     .build(); 
domainSearchWebServices = retrofit.create(DomainSearchWebServices.class); 

मैं प्रतिक्रिया jsonschema2pojo साथ JSON नियंत्रित किया है अगर मैं अपने प्रतिक्रिया वर्ग राइट modled और यह ठीक लगता है देखने के लिए।

क्यों रेट्रोफिट मेरी प्रतिक्रिया को बदलने में विफल रहता है?

अद्यतन

अभी के लिए एक काम के आसपास मैं त्रुटि शरीर से मेरी प्रतिक्रिया का निर्माण कर रहा हूँ के रूप में।

+0

क्या आपने जेसन कनवर्टर सेट किया था? 'RestAdapter restAdapter = new RestAdapter.builder() .setConverter (नया GsonConverter (gson)) ' – ProblemSlover

+0

@problemsolver हाँ मैंने किया था। मैंने प्रासंगिक कोड जोड़ा है। –

+0

@problemsolver मैंने 'addConverterFactory' का उपयोग नहीं किया है 'सेट कनवर्टर' –

उत्तर

29

मैंने समस्या हल कर दी है। जब मैं एक बुरा अनुरोध करता हूं (HTTP 400) रेट्रोफिट प्रतिक्रिया को परिवर्तित नहीं करता है। इस मामले में आप response.errorBody.string() के साथ कच्ची प्रतिक्रिया तक पहुंच सकते हैं। इसके बाद आप एक नया जीसन बना सकते हैं और मैन्युअल रूप से इसे परिवर्तित कर सकते हैं:

if (response.code() == 400) { 
    Log.d(TAG, "onResponse - Status : " + response.code()); 
    Gson gson = new Gson(); 
    TypeAdapter<RegisterResponse> adapter = gson.getAdapter(RegisterResponse.class); 
    try { 
     if (response.errorBody() != null) 
      registerResponse = 
       adapter.fromJson(
        response.errorBody().string()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
+1

'त्रुटि बॉडी' के लिए क्रैश करता है। –

+0

@ इगोरगानापोलस्की आपको हमेशा यह जांचना चाहिए कि शरीर या त्रुटि निकाय शून्य है या नहीं। –

+0

मुझे एक समान समस्या है, क्या आप कृपया सलाह दे सकते हैं? http://stackoverflow.com/questions/39261481/assigning-reference-when-accessing-json-response – tccpg288

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