如何从翻新中读取字符串响应

时间:2018-10-29 11:33:46

标签: android string retrofit response

我需要根据改造响应来处理功能。

post方法的请求为json格式,响应为Text true

我试图通过以下代码片段获得此响应。但是尽管邮递员的回复是 true ,但我总是得到 false

Click to view the response

private void callPostLoginAPI(String webServiceResponse) {
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<ResponseBody> result = apiService.getPostDealer(postLoginAPI(webServiceResponse));
result.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
String postLoginResponse = null;
try {
postLoginResponse = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
 if (postLoginResponse != null || (!postLoginResponse.equals(""))) {
                    if (postLoginResponse.equals("true")) {
                        try {
                            if (PreferenceClass.getInstallationID(Loginpage.this) == null ||
                                    PreferenceClass.getInstallationID(Loginpage.this).equals("")) {
                                request_appInstallation_API(0);
                            } else {
                                checkAppUpdate();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        Toast.makeText(Loginpage.this, "Please contact CMS Admin", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(Loginpage.this, "Something went wrong... Please try again", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(Loginpage.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

更新:

在尝试以下操作时,我还没有获得相应的“邮递员响应值”。

ApiInterface apiService = ApiClient.getClient1().create(ApiInterface.class);
        Call<Boolean> result = apiService.getPostDealer(postLoginAPI(webServiceResponse));
        result.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                Log.i("Response", response.body().toString());

                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        Log.i("callPostLoginAPI", response.body().toString());
                        Toast.makeText(Dealer_Loginpage.this, "returned", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(Dealer_Loginpage.this, "Nothing returned", Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                Toast.makeText(Dealer_Loginpage.this, "Nothing returned", Toast.LENGTH_LONG).show();
            }
        });

ApiClient.getClient1()

public static Retrofit getClient1() {
    if (retrofit1 == null) {
        retrofit1 = new Retrofit.Builder().baseUrl(GlobalClass.sBase_Url).
                addConverterFactory(ScalarsConverterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).build();
    }
    return retrofit1;
}

1 个答案:

答案 0 :(得分:0)

解决方案:

附加:

postLoginResponse.replaceAll("[^A-Za-z]+", "");

行后:

postLoginResponse = response.body().string();

然后尝试。