1
我用改装来了解从API响应处理来自服务器的多个类型的反应,但我得到了不同类型从相同的API响应的,像这些如何通过改造
- 的JSONObject
- 字符串键入
- 布尔类型
基于这种情况,它给不同类型的来自同一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 $
如果我将使用回调时,我想的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
如果您总是*获得JSON响应,您应该只使用Retrofit **与Gson转换器** –
相关:https: //stackoverflow.com/questions/32617770/how-to-get-response-as-string-using-retrofit-without-using-gson-or-any-other-lib –