Android : exception handling - android

My android aplication tries to connect to a service using
response = client.execute(getRequest);
However the server might be down and at such times I would like to throw a custom exception with a custom message, rather than the message android provides, saying the application has shut down unexpectedly.
Is there any way to do this?

try
{
response = client.execute(getRequest);
}
catch(Exception ex)
{
// Or add your custom exception here
Log.e("Your Custom Message:",ex.toString());
}

You can try wrapping the call in a try-catch block, and catch the generic Exception type, then process it (see if the server is down), then throw your custom exception.
try
{
response = client.execute(getRequest);
}
catch(Exception e)
{
//processing
throw new MyCustomException();
}

Write your own class MyException extends Exception{... and give some custom implementation to it. That can be in the constructor of that class or in some method.
And throw it like -
try{
response = client.execute(getRequest);
}catch(Exception e){
//handle your exception
throw new MyException();
}

If you also wish to show this custom message to the user, you can do so this way:
try {
response = client.execute(getRequest);
} catch(Exception e) {
Toast.makeText(getApplicationContext(), "My custom message", Toast.LENGTH_LONG).show();
throw new Exception("My custom message", e);
}

Can you print the e.printstacktrace(); in exception block?
I think there you will get details on what type of exception you are getting.

I solved this problem using ACRA
http://code.google.com/p/acra/wiki/BasicSetup
http://code.google.com/p/acra/wiki/AdvancedUsage
You can overwrite the default crash report and display a custom toast instead using
#ReportsCrashes(formKey="dGVacG0ydVHnaNHjRjVTUTEtb3FPWGc6MQ",
mode = ReportingInteractionMode.TOAST,
forceCloseDialogAfterToast = false, // optional, default false
resToastText = R.string.crash_toast_text)

Related

Can't get a string from nested downloaded JSON [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 5 years ago.
I'm getting a JSON from the server:
JSONObject vkjson = response.json;
I have tried to make it a string and check via LogCat to make sure it works - and yeah, it 100% works.
But it is nested:
{"response":{"first_name":"...","last_name":"..."} }
I have tried to do this:
String result = vkjson.getJSONObject("response").getString("first_name");
But IDE doesn't like the getJSONObject part and underlines it. IDE says:
Unhandled exception in org.json.JSONException
What's wrong? Is it because the JSON is loading from the server or the code is incorrect?
Thank you in advance.
Unhandled exception in org.json.JSONException
Mean that the method can throw a JSONException and you have to handle it.
So you have to:
try {
String result = vkjson.getJSONObject("response").getString("first_name");
} catch (JSONException exception){
//Handle exception here
}
There's nothing wrong with your code other than not handling the JSONException which is potentially thrown (i.e. what would happen if there isn't an object called "response").
You need to look at exception handling and wrap this code in a try .. catch block or otherwise deal with the exception.
Java exception handling
Do this-:
try
{
JSONObject vkjson = response.json;
//More code related to json
}
catch(JsonException e)
{
e.printStackTrace();
}
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(responseString);
if(jsonObject != null)
{
if (jsonObject.has("response")) {
JSONObject responseObject = jsonObject.getJSONObject("response");
String firstName = responseObject.getString("first_name");
Log.d("Tag",firstName);
}
}
} catch (JSONException e) {
e.printStackTrace();
}

Android - JSON No value for 1 - but there is using Log

I'm stuck at trying to get a simple string from a JsonObject.
Here is the basic code. result is a JsonObject returned from an Asynctask. I've done dozens of the same way, and here it doesn't work.
Response:{"status":200}
Error: An error is thrown saying "No value for 1"
When I Log the content of the JsonObject, I get this: 200, which is what I want. For which reason could this return null when there is a value?
Edit if I use the result.has("status"); true is returned. I don't understand.
Log.e("TAG", result.get("status").toString());
try{
String status = result.getString("status");
} catch (Exception e){
e.getMessage();
}
You are getting int value in the object and you are holding in string which causes the error
You are getting int value in status
{
"status":200
}
if it is like
{
"status":"200"
}
Your code works perfectly.
Try this
Log.e("TAG", result.get("status").toString());
try{
int status = result.getInt("status");
} catch (Exception e){
e.getMessage();
}

Weird JSON encoding issue on Android 4.1.2

I have an issue on Android 4.1.2 where a JSON object given to us by our REST API gets encoded weirdly when sending back.
This is the snippet of json I'm getting:
"cost":{
"amount": 0,
"currency": "GBP"
}
I'm wanting to pretty much just pass this particular snippet back the same way (modifying other parts of the json), but this is what I get on Android 4.1.2:
"cost":"{amount=0, currency=GBP}"
The function I believe is causing this weird encoding is here:
private StringEntity getEntityForRequest(final Payment payment, final PaymentDelegate delegate) {
JSONObject json = new JSONObject();
MyApplication.getContext().addApplicationInformationToJSONObject(json);
StringEntity entity = null;
try {
entity = new StringEntity(json.toString(), "UTF-8");
} catch (UnsupportedEncodingException e1) {
payment.markAsFailed("Reservation failed, data returned not expected.");
save(payment);
if (delegate != null) {
delegate.onFailure(new MyError(MyError.DEFAULT_STATUS, MyError.DEFAULT_TYPE, "Payment error", "Error during reservation"));
}
}
return entity;
}
This is the addApplicationIformationToJSONObject function:
/**
* Adds system information to a JSON object.
*/
public void addApplicationInformationToJSONObject(JSONObject json) {
try {
try {
json.put("app_version", getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
} catch (NameNotFoundException e) {
json.put("app_version", "Unknown");
}
json.put("device", getDeviceName());
json.put("os_type", "android");
json.put("os_version", String.format("%d", Build.VERSION.SDK_INT));
json.put("device_id", Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID));
} catch (JSONException e) {
MyLog.e("MyApplication", "Error when adding system information to JSON");
}
}
What's causing this weird encoding?
How can I modify the code to avoid issues like this?
Found a solution. It seems older version interprets that cost snippet as a string rather than a JSONObject. Doing this seems to solve the issue:
ticketObject.remove("cost");
ticketObject.put("cost", new JSONObject(getCost()));

Retrofit not returning any response

I am trying to use RetroFit Synchronus call to connect and fetch data to one of my APIs.
try{
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(finalUri.toString()).build();
IGooglePlacesApi iGPlaceApi = restAdapter.create(IGooglePlacesApi.class);
mGooglePlacesApiResponse googlePlacesObj = iGPlaceApi.getStreams();
RetrofitError retrofitError;
} catch (IOException e) {
serverResponse = e.getMessage();
} catch (IllegalArgumentException e) {
serverResponse = e.getMessage();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Log.d("serverResponse:", serverResponse);
}
Which is declared here as:
public interface IGooglePlacesApi {
#GET("/stream/list.json")
mGooglePlacesApiResponse getStreams();
}
Issue is when i call iGPlaceApi.getStreams(); i dont get a result neither any error. But my code just directly goes to the finally block?
Why this is happening, no result, no catch. How can i correct this?
Retrofit wraps all internal exceptions into a RetrofitError exception and throws that. In your case you're not catching that exception therefore only the finally block is executed.
Try modifying your code this way,
try {
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(finalUri.toString()).build();
IGooglePlacesApi iGPlaceApi = restAdapter.create(IGooglePlacesApi.class);
mGooglePlacesApiResponse googlePlacesObj = iGPlaceApi.getStreams();
} catch (RetrofitError e) {
serverResponse = e.getMessage();
} finally {
Log.d("serverResponse:", serverResponse);
}
The RetrofitError thrown depending on the failure will contain the exception cause or the status reported by the server. You can then apply the necessary logic from there.
For more general purposes error handling you can set an ErrorHandler when creating your RestAdapter. This ErrorHandler will be executed before you catch the RetrofitError in your try/catch.

Need some explaining

So this is the weirdest thing ever to happen to me during programing. Yes I'm no pro at programing, but I'm learning as I go. I've got an app talking to a server, a socket in the main thread, reading is done in a separate class and thread and writing in a separate class with asynctask.
The problem is LocationManager. I could talk to server and write/read commands just fine, I implemented the LocationManager and its listener.
I then proceeded to implement a method to update my textview with the new coordinates on locatinChanged. So far so good. Thing is when I use the Emulator control in eclipse and send coordinates the app crashed with a stringOutOfBoundsException (I've programed for 3 years now never seen this). I looked at the code stepped through it and so on. Read the stacktrace, logcat, console and everywhere I could think of but it got me nowhere. Until I finally went to the readerthread which looks like this:
public class ReaderThread extends Thread {
public void run() {
new Thread(new Runnable(){
public void run(){
try {
//Establish a bufferedreader to read from the socket/server.
in = new BufferedReader(new InputStreamReader(socket.getInputStream()), 8 * 1024);
}
catch(Exception e) { e.printStackTrace(); }
//As long as connect is true.
while (connected) {
String line;
try {
//Try to read a line from the reader.
line = in.readLine();
System.out.println(in.readLine());
if (in == null) {
//No one has sent a message yet.
System.out.println("No data recieved");
}
else {
int i = 0;
//As long as someone is sending messages.
while((line = in.readLine()) != null ){
//Make a new Message.
Message msg;
msg = new Message();
//Set the object to the input line.
msg.obj = line;
//Set an id so it can be identified in the main class and used in a switch.
msg.what = i;
System.out.println("i is: "+i);
//Send the message to the handler.
Main.this.h.sendMessage(msg);
}
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}).start();
}
The variable i is in an if statement depending on what the server sent but I cut that out as it has nothing to do with this problem.
The problem is the freaking catch. When the catch is IOException, the app crashes. Out of dumb luck I changed this to Exception and printed e.message to catch the error and see what caused it. Thing is this change fixed it. How can switching IOException to just plain Exception fix a problem like this?
Its like with IOException the program says: "hey your not gonna catch the error but there is no error" but with Exception it says "Well now you could catch it so I'll proceed".
My app is working but I just can't grasp this, why and how does this happen?
You're essentially telling the application to catch the base Exception class. This means that any type of error will be caught, since all exception classes descend from that base type. Since StringOutOfBoundsException does not descend from IOException it was not being caught before and the error was not being caught. Instead of catching all exceptions, you might try the following:
try {
// Your code here...
} catch (IOException e) {
Log.e(TAG, "Caught an IOException!", e);
} catch (StringOutOfBoundsException e) {
Log.e(TAG, "Caught a string out of bounds Exception!", e);
}
I'm unable to determine what is actually throwing the StringOutOfBoundsException to begin with. It may be in the if statement that you cut out of your example.
while (connected) {
String line;
try {
//Try to read a line from the reader.
line = in.readLine();
System.out.println(in.readLine());
if (in == null) {
//No one has sent a message yet.
System.out.println("No data recieved");
}
The test for in == null is in a funny location. You should receive a NullPointerException if that test were to ever return true by nature of calling methods on it a few lines earlier. Obviously something is a little funny with this code.
You fail to save the return value from in.readLine() the second time you call it. I hope it did not contain anything useful. (Though, since you print the line, you obviously wanted to know what data it contained.)
Whatever that line was (from the first call to in.readLine()), it gets thrown away; there's nothing else in the loop that uses it before it is over-written on this line:
while((line = in.readLine()) != null ){
At this point, the two lines that you read are gone forever.
I'm not entirely sure what should be done to fix this; if it were me, I'd be sorely tempted to start over with a sheet of paper and sketch out what the method should be doing without looking at the existing code, then compare the sketch against the code to see which cases each one has overlooked.

Categories

Resources