Error response from Razorpay getting empty - android

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

Related

Facing android volley lib error response issue

I have integrated android volley lib for node js rest service calls. If I get response success it returns status code 200 and response JSON object which is working.
But if I get any 400(401,409, etc) and 500(500,504, etc) related error it is not showing error response coming from service instead it showing
Volley.error(service sending {status:false,message:"enter password"}
this error object but I am unable to read this message).
How to read error response coming from node js service using volley lib.
In onErrorResponse() method write the below code:
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null && networkResponse.data != null) {
try {
String jsonError = new String(networkResponse.data);
Log.d(TAG, "jsonError: " + jsonError);
JSONObject errorObject = new JSONObject(jsonError);
// Here you can do whatever you want with JSONError.
Snackbar snackbar = Snackbar.make(mActivitySingleSellerBinding.relativeLayoutRoot, errorObject.getString("message"), Snackbar.LENGTH_LONG);
snackbar.show();
} catch (JSONException e) {
e.printStackTrace();
}
}
This errorObject.getString("message"); will give you the error string.

Android Studio Weird error with volley and response from volley

Am I missing something, I am trying to create a loginActivity and I created this code, and I have a log that logs the answer to the volley I created...But The if statement is not getting entered...Can someone see something I'm doing wrong? Sorry if its really dump( and yes I tried to change the response.equals check to be response.equals(" Password Accepted!");, this did not work either)
Another thing to note, yes I know my search in the log is different then the tag, it doesnt login, (which is what i want it to do)
Toast.makeText(getApplication(), response, Toast.LENGTH_LONG).show();
Log.e("Before Conversion",response);
String convertedResponse = null;
try {
convertedResponse = new String(response.getBytes("ISO-8859-1"), "UTF-8");
L.e("After Conversion",convertedResponse);
if (convertedResponse.equals("Password Accepted!")) {
showProgress(false);
Message msg = new Message();
msg.what = 1;
msg.obj = succcess ? 1 : 0;
handler.sendMessage(msg);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
06-29 15:24:04.990 16707-16707/E/Before Conversion: Password Accepted!
06-29 15:24:04.990 16707-16707/E/After Conversion: Password Accepted!
After suggestion to change logging...
06-29 15:34:32.339 16707-16707/ E/Got a response:: Password Accepted!
UPDATE
After creating a JSON array of length 1 with just the response string and a key for it, and passing it to my android studio code, the json array wont parse through the functions used to parse it (JSON Object, JSON Array), but manually parsing the response string works..... Less graceful then I would like it but i guess it works....
This can happen when encoding of response is not UTF-8. Try converting response like this:
String convertedResponse = null;
try {
convertedResponse = new String(response.getBytes("ISO-8859-1"), "UTF-8");
// do your things ...
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
your response is in JSONObject not JSONArray, so on Response Listnere use:
if(response.getString("response").equals("PASSWORD ACCEPTED!"){
//do what you want to do
}

Retrofit error message is broken

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");

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");
}

Parsing JSON webservice result android

This is part of an AsyncTask that calls a web service that returns a JSON result. As you can see I hard coded the actual JSON return object. This was pulled directly from the error I got that specified it could not create the JSON object from the string object I was passing in, which is the variable result. That error occured when it hit new JSONObject(result) in the ParseResults method. Why would hard coding the exact string work but not the string being passed in?
#Override
protected void onPostExecute(String result) {
try {
result = "{\"Response\":{\"ReturnCode\":200,\"ReturnMessage\":\"Information Successfully Retrieved\",\"ReturnData\":null,\"ReturnClass\":{\"PRO_ID\":\"11111111-1111-1111-1111-111111111111\",\"PRO_FirstName\":\"SILVER\",\"PRO_LastName\":\"HIYO\"},\"FriendlyErrorMessage\":null}}";
JSONObject jsonObject = new ApiMethods().ParseResult(result);
ParseResults method snippet.
public JSONObject ParseResult(String result) throws JSONException
{
JSONObject returnedObject = new JSONObject();
try
{
JSONObject jsonObject = new JSONObject(result);
Also below, as i stated in a comment to another user, is the return statement that is returning the data. This is being returned from a .NET MVC application. I added in the UTF8 when that was mentioned and still get the same error.
return Json(data: new { Response = returnValue }, contentType: "application/json", contentEncoding: System.Text.Encoding.UTF8, behavior: JsonRequestBehavior.AllowGet);
And the entire error message:
org.json.JSONException: Value {"Response":{"ReturnCode":200,"ReturnMessage":"Information Successfully Retrieved","ReturnData":null,"ReturnClass":{"PRO_ID":"11111111-1111-1111-1111-111111111111","PRO_FirstName":"Silver","PRO_LastName":"HIYO"},"FriendlyErrorMessage":null}} of type java.lang.String cannot be converted to JSONObject
Seems like your hardcoded json object is not a valid json object. This may be the reason why it throws exception. Check validitiy of json object here first.
type java.lang.String cannot be converted to JSONObject
This means "Use getString() for String"
getJSONObject() may cause this error.
class Response {
String returnMessage;
...
}
Response response;
response.returnMessage= "msg";
JSONObjct obj;
obj = response.getJSONObject("ReturnMessage"); // cannot be converted
It maybe a encoding problem. Browser (and source editor) may have converted the result string encoding.
Q: ... I am storing items for the JSON data as Strings which is resulting in some odd character appearing
A: new String(jo.getString("name").getBytes("ISO-8859-1"), "UTF-8");
Android JSON CharSet UTF-8 problems
Hard coded JSON string is valid. If you want to try, replace (\") with (") and paste it to the checker.
{
"Response": {
"ReturnCode": 200,
"ReturnMessage": "Information Successfully Retrieved",
"ReturnData": null,
"ReturnClass": {
"PRO_ID": "11111111-1111-1111-1111-111111111111",
"PRO_FirstName": "SILVER",
"PRO_LastName": "HIYO"
},
"FriendlyErrorMessage": null
}
}
JSON object is like a structure (or class)
It looks like this.
class Response {
int ReturnCode = 200;
String ReturnMessage = "Information Successfully Retrieved";
...
}
Sample code.
protected void onPostExecute(String result)
{
JSONObject jsonObject;
JSONObject response;
int returnCode;
String returnMessage;
//JSONObject returnMessage;
result = "{\"Response\":{\"ReturnCode\":200,\"ReturnMessage\":\"Information Successfully Retrieved\",\"ReturnData\":null,\"ReturnClass\":{\"PRO_ID\":\"11111111-1111-1111-1111-111111111111\",\"PRO_FirstName\":\"SILVER\",\"PRO_LastName\":\"HIYO\"},\"FriendlyErrorMessage\":null}}";
try
{
jsonObject = new JSONObject(result);
response = jsonObject.getJSONObject("Response");
returnCode = response.getInt("ReturnCode");
returnMessage = response.getString("ReturnMessage");
//returnMessage = response.getJSONObject("ReturnMessage"); // This may cause same error
}
catch (JSONException e)
{
e.printStackTrace();
}
}
Use this site to validate your json string
http://jsonlint.com/

Categories

Resources