2017-07-26 58 views
1

我用改装来了解从API响应处理来自服务器的多个类型的反应,但我得到了不同类型从相同的API响应的,像这些如何通过改造

  1. 的JSONObject
  2. 字符串键入
  3. 布尔类型

基于这种情况,它给不同类型的来自同一API响应。
我试图用这个代码:

serverUtilities.getBaseClassService(getApplicationContext(), "").forgotPasswordSecurityAnswerCheck(in, new Callback<JsonObject>() { 
     @Override 
     public void success(JsonObject s, retrofit.client.Response response) { 
      Log.d("forgot_password_respons", "--->" + "" + s.toString()); 

      /* to retrieve the string or integer values*/ 

      if (s.toString().equals("5")) { 
       utilities.ShowAlert("selected wrong question", "Forgot Password SecQues"); 

      } 
      if (s.toString().equals("1")) { 
       // some alert here 

      } 

      /* to retrieve the boolean values*/ 

      if (s.toString().equals("false")) { 
       utilities.ShowAlert(getResources().getString(R.string.otp_fail), "Forgot Password SecQues"); 

      } 

      if (s.toString().equals("1")) { 
       utilities.ShowAlert("Email already registered", "Social Registration"); 

      } 
     /*to retrieve the Json object*/ 

     else 
      if (s.toString().charAt(0) == '{') { 
       Log.d("my_first_char", "" + s.toString().charAt(0)); 

       try { 
        if (s.toString().contains("memberId")) { 
         String MemberId = s.get("memberId").getAsString(); 
         String optId = s.get("otpId").getAsString(); 
         Log.d("Forgot_pass_ques_act", "" + MemberId + "--->" + optId); 

         Singleton.setSuccessId(MemberId); 
         Singleton.setOptId(optId); 
         Intent intent = new Intent(ForgotPasswordQuestionActivity.this, PasswordActivity.class); 
         startActivity(intent); 
         Toast.makeText(ForgotPasswordQuestionActivity.this, "congrats!! second step Success ", Toast.LENGTH_SHORT).show(); 

        } else if (s.toString().contains("mId")) { 

        } 
       } catch (Exception e) { 
        utilities.ShowAlert(e.getMessage(), "forgot passwordQues(catch)"); 

        Log.d("forgot_password_error", "" + e.getMessage()); 
       } 


       // Singleton.setSuccessId(s); 
      } 
     } 

     @Override 
     public void failure(RetrofitError error) { 
      utilities.ShowAlert(error.getMessage(), "forgot passwordQues(server)"); 

      Log.d("forgot_passwo_secAnser", "--->" + error.getMessage()); 

     } 
    }); 

在这里,我的电话留了返回类型“的JSONObject”回来,并转换到一个并检查它是否是一个的JSONObject布尔字符串并执行相关操作。
但我得到这个例外同时处理对策:

响应:

 com.google.gson.JsonSyntaxException:Expected a com.google.gson.JsonObject but was com.google.gson.JsonPrimitive 

任何人都可以建议我如何处理在改造单一类型的回调,这些反应?

如果我用一个字符串类型这样的回调:

server_utilities.getBaseClassService(getApplicationContext(), "").forgotPasswordResponse(in, new Callback<String>() { 
     @Override 
     public void success(String s, retrofit.client.Response response) { 
      Log.d("forgot password resp", "--->" + "" + s.toString()); 

     } 

     @Override 
     public void failure(RetrofitError error) { 


      Log.d("forgot_password_error", "--->" + error.getMessage()); 

     } 
    }); 
} 

我收到此错误:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $ 

回答

0

服务器不能给你一个Boolean对象,并改造为自动将字符串转换为JSONObject,因为这是您告诉的类型

要停止该操作,只需请求一个字符串,并稍后解析它

new Callback<String>() 

或许

new Callback<JsonPrimitive>() 

您还可以得到response.raw属性(这是在改造2.X)

+0

如果我将使用回调时,我想的JSONObject:它给人的follwing错误COM .google.gson.JsonSyntaxException:java.lang.IllegalStateException:期望一个字符串,但在BEGIN_OBJECT行1列2路径$ ....如果我将使用回调当我想要“字符串”它会给--- --- com .google.gson.JsonSyntaxException:java.lang.IllegalStateException:期望一个字符串,但是BEGIN_OBJECT在第1行第2列路径$ ---->我不知道如何处理布尔响应 – uma

+0

如果您总是*获得JSON响应,您应该只使用Retrofit **与Gson转换器** –

+0

相关:https: //stackoverflow.com/questions/32617770/how-to-get-response-as-string-using-retrofit-without-using-gson-or-any-other-lib –