Retrofit error message is broken - android

onResponse is called.
But response.isSuccessful() is false.
I want to watch error massage.
#Override
public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
if (!response.isSuccessful()) {
try {
Log.d("Success false", response.errorBody().string()); // letter broken!!
} catch (IOException e) {
e.printStackTrace();
}
return;
}
}
print :
{"error":"\ub85c\uadf8\uc778\uc774 \ud544\uc694\ud569\ub2c8\ub2e4."}
Why letter is broken?

I think the error message is UTF8 encoded string. You could decode it by URLDecoder.decode(s, s)

I fixed it!
I think of json data as a string data.
error massage is json data.
json data need convert.
Try this :
JSONObject jsonobject = new JSONObject ( response.errorBody().string());
Log.d("error",jsonobject.getString("error");

Related

Error response from Razorpay getting empty

I'm using RazorPay sdk 1.6.18 in android mobile app
#Override
public void onPaymentError(int code, String response, PaymentData data) {
try {
System.out.println("Payment Error");
String errorMsg = null;
JSONObject jsonObject = new JSONObject(response);
if(jsonObject.has("error")) {
JSONObject errorJson = jsonObject.getJSONObject("error");
if(errorJson.has("description"))
errorMsg = errorJson.getString("description");
}
Toast.makeText(this, errorMsg, Toast.LENGTH_LONG).show();
} catch(Exception e) {
FirebaseCrashlytics.getInstance().recordException(e);
}
}
in onPaymentError() String response is getting empty & it's throwing exception
org.json.JSONException
End of input at character 0 of
We got this issue from crashlytics & it's affecting few users in production.
Does anybody have any clue in which case response will be empty & what's a better way to handle this?
Just Try,
Response strings must be checked for empty, null, or "null" status before being assigned a JSON object. then turn the response string into a JSON object. able to prevent crashes

Gson does not parse json String

Now I'm working on an Android SDK project, the code is too old and I tried to collect data from server using okhttp3. By using the following function I'm able to collect the response from server.
String json = requestGetString(url);
public String requestGetString(String uri) throws IOException {
Response res = null;
try {
res = getStringHttp(host+uri);
System.out.println(res.body().string());
return res.body().string();
}catch (NullPointerException e) {
//body = null ??
throw new IOException(e);
} catch (IOException e) {
throw new IOException(e);
}finally {
if(res != null){
try{
res.close();
}catch (NullPointerException e){
//body = null ??
throw new IOException(e);
}
}
}
}
I'm receiving the Json but I'cant parse it with my data class.
like the following
AuthAppResponse result = parseJson(path, json, AuthAppResponse.class);
You can look my parser function
private <TResponse> TResponse parseJson(String path, String json, Class<TResponse> responseClass) throws IOException
{
return parseJson(getGson(), path, json, responseClass);
}
and this,
private <TResponse> TResponse parseJson(Gson gson, String path, String json, Class<TResponse> responseClass) throws IOException
{
try
{
return gson.fromJson(json, responseClass);
}
catch (JsonSyntaxException e)
{
throw new IOException("The response from '" + path + "' failed to be parsed as JSON.\ncontent = " + json, e);
}
}
Nothing happen.
And the same SDK with latest code base it works fine with the same code. Anyone have an idea to figure out the possible solution for this.
please share your comment.
You can consume an okhttp response body only once. You're consuming it twice. First in:
System.out.println(res.body().string());
and then in
return res.body().string();
Finally i fount the missing part in my code, I miss to add a proguard file in the old project, that's why the Gson does not parse the response

Android json parsing can get json response from ws calling from Retrofit?

I am using retrofit lib because it's giving fast response.
But why need always one model class corresponding one ws response of parsing/calling web service using retrofit. Is it possible to get json response directly in my program?
You can wrap your response inside Response and then access the JSON data using the following:
call.enqueue(new Callback<ResponseBody>() {
#Override
onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
// Do your success stuff...
} else {
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
Toast.makeText(getContext(), jObjError.getString("message"), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
Yes, you can. Use this as your api call:
Call<ResponseBody> yourMethod(). Then in your onResponse callback you can get json response via response.body().string(). See javadoc.

Can't Log OKHTTP3 response.body.string

OK- so I'm a bit confused by this.
I've setup an OKHTTP3 POST. When I get the response I've been trying to put the body into a string (it's a string response) but something is going wonky:
try {
Response response = client.newCall(request).execute();
String rep = response.body().string();
if(rep.length() > 0){
Log.i(TAG, "Got Response");
Log.i(TAG, rep);
}
}catch (Exception e){
e.printStackTrace();
}
I get a Log saying "Got Response" but (the length is 80) then it just stops. It doesn't say my rep string is null, or empty... It just never calls that second Log.
Anyone have any kind of idea what's up?
I also met this problem, Log can't work.
And I found a temporary solution.
I try to use Toast to show the response, then I found that the response poped up, but with a lot of spaces(not sure if those characters are spaces).
So I am using Log.i(TAG, rep.trim());, which works well. I don't know if this solution is good for this problem, but it works for me after all.
Try using this..
OkHttpClient client = new OkHttpClient.Builder().build();
Request request = new Request.Builder()
.url("")
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
try {
Log.d("Response",response.body().string());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
response.body().close();
}
}
});
Right... So apparently I just needed to put the part that gets the string in an AsyncTask, and it started working.
There wasn't any error message or anything indicating to do so, but I went back and actually looked at the documentation for the Callback, and it mentions consuming the body on another thread.
new SendPictureClass().execute(response);
Probably should have started there to begin with... ¯_(ツ)_/¯
Thanks for the help guys!

How to Check Web service response is a valid json or not programmatically in android?

I am using web services in my application. I use REST services. When I call the web service I get a 200 Response code and correct json response too. But sometimes I get a 200 response code along with an non-json response. So before parsing I need to check whether its a valid json or not. Pls help me.
You can check Json String with the help of following method,
public boolean isJSONValid(String json)
{
try
{
new JSONObject(json);
return true;
}
catch(JSONException ex)
{
return false;
}
}
Use try catch block. Like this:
try {
JSONObject jsonObject = new JSONObject("yourjsonstring");
}
catch(JSONException j) {
System.out.println("Not a JSON");
}

Categories

Resources