I am using AsyncTask to get the data form a web service that I created. Now I want to display proper error messages to user like if the Internet is not available it will display toast for that similarly if server is down it will display toast for that. I want to set a string with error like "server is down" or "internet problem occured" in doInBackground() and display Toast in onPostExecute() but I want to know if my server is down that what exception is thrown? and if my server is active but during transfer internet is disconnected so what exception is thrown ?
Probably modelling the response is the best and easiest way.
For example,
you can compose a model from the data you got such as:
class ApiResponse {
public final String responseString;
public final Throwable error;
public ApiResponse (String responseString,Throwable error){
this.responseString = responseString;
this.error = error;
}
}
Then, you can bind response or error to that model and return from doInBackground();
Pseudo code :
class ApiAsyncTask extends AsyncTask<Void, Void, ApiResponse> {
....
protected ApiResponse doInBackground() {
try {
//callApi and get response, if success pass null in error
return new ApiResponse(responseString, null)
} catch (Exception e) {
e.printStackTrace();
//otherwise pass that error
return new ApiResponse(null, e);
}
}
protected void onPostExecute(ApiResponse apiResponse) {
//now just need to check if error is null
if (error == null) {
String json = apiResponse.responseString;
//parse response
} else {
//get error and check with instanceOf
Throwable error = apiResponse.error;
if (error instanceOf SocketTimeoutException){
//show timeout error
}
else if (error instanceOf SomeXYZException){
//handle that exception
}
}
}
}
This is just an example. You can put anything you want in ApiResponse and compose model of that data. (such as status code got from api, generated pojo class of json response by some converter etc. anything). Once you have the data bounded, you can use it in onPostExecute() as it will always be running on your UI thread. Note that Third type param Result of AsyncTask is built for that by definition : AsyncTask<Params, Progress, Result>.
Catch exceptions of your doInBackground() to string and than, depends what you need, you can show toast message from onPostExecute() method, something like this:
#Override
protected Void doInBackground(Void... arg0) {
try {
// do you stuff here
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
e.printStackTrace();
response = "IOException: " + e.toString();
} finally {
...
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void onPostExecute(Void result) {
if (response.contains("IOException: java.net.ConnectException:")) {
Toast.makeText(getApplicationContext(), "YOUR TEXT HERE", Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();
}
else if (response.contains("IOException: java.net.SocketTimeoutException:")) {
Toast.makeText(getApplicationContext(), "YOUR TEXT HERE", Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();
}
else if (response.contains("IOException: java.net.SocketException:")) {
Toast.makeText(getApplicationContext(), "YOUR TEXT HERE", Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();
}
}
Of course, this is yust an example, but you could see how that can be worked out.
I'm using azure sdk for android and follow the tutorial https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-android-get-started-data/.
When I'm trying to connect and insert data to mobile service table all is ok, but when I query the table in activity my app gets stuck, though there are only several entries in the table and execute method successfully returns Future.
public static MobileServiceClient mClient;
public static void connect(Context context) {
try {
mClient = new MobileServiceClient(storageLink, key, context);
} catch (MalformedURLException e) {
Log.e("AzureService.connect", "Storage access failed" + storageLink);
}
}
public static InstallationData get(final String deviceId) {
MobileServiceTable<InstallationData> table= mClient.getTable(InstallationData.class);
final MobileServiceList<InstallationData> result;
try {
result = table.where().field("deviceid").eq(deviceId).execute().get();
for (InstallationData item : result) {
return item;
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return null;
}
public static void store(final InstallationData item) {
mClient.getTable(InstallationData.class).insert(item, new TableOperationCallback<InstallationData>() {
public void onCompleted(InstallationData entity, Exception exception, ServiceFilterResponse response) {
if (exception == null) {
Log.d("AzureService.store()", "Data about " + item.getDeviceid() + "" + "is successfully updated");
} else {
exception.printStackTrace();
Log.e("AzureService.store()", "Data about " + item.getDeviceid() + "" + "is failed to update");
}
}
});
}
Thank you in advance!
"There i found index out of bound exception how can i solve???"
public static void addToCart() {
ArrayList<ItemData> iArr;
if (holdSelection != null) {
String categoryID = holdSelection.getCategoryID();
int position = categoriesAddedd.indexOf(holdSelection.getCategoryID());
int itemPosition =categoriesitemAddedd.indexOf(holdSelection.getItemData().get(0).getItemID());
if (!categoriesAddedd.contains(categoryID)) {
CategoryData data = new CategoryData();
data.setCategoryID(categoryID);
data.setCategoryName(holdSelection.getCategoryName());
data.setItemData(holdSelection.getItemData());
mAddedToCart.add(data);
categoriesAddedd.add(categoryID); categoriesitemAddedd.add(holdSelection.getItemData().get(0).getItemID());
Toast.makeText(mContext,R.string.Add_to_cart,Toast.LENGTH_SHORT).show();
} else {
if (position>=0) {
if(itemPosition>=0){
if(holdSelection.getItemData().get(0).getScaled()){
Toast.makeText(mContext,R.string.Already_in_cart,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mContext,R.string.Already_in_cart,Toast.LENGTH_SHORT).show();
}
}else{
try{
CategoryData data = mAddedToCart.get(position);
iArr = data.getItemData();
iArr.add(holdSelection.getItemData().get(0));
categoriesAddedd.add(categoryID);
categoriesitemAddedd.add(holdSelection.getItemData().get(0).getItemID());
Toast.makeText(mContext, R.string.Add_to_cart,Toast.LENGTH_SHORT).show();
}catch (Exception e) {
// TODO: handle exception
Toast.makeText(mContext, "There is " +e,Toast.LENGTH_SHORT).show();
System.out.println("Exception : "+ e.getMessage());
e.printStackTrace();
}
}
}
}
}
}
It's impossible to help without a minimum of (structured) information (and also without a Hello, please, tank you, polite words if you prefer).
First of all, you have to use the brackets to display code when you post, so your clean code should appear as follow :
public static void addToCart() {
ArrayList iArr;
if (holdSelection != null) {
String categoryID = holdSelection.getCategoryID();
int position = categoriesAddedd.indexOf(holdSelection.getCategoryID());
int itemPosition =categoriesitemAddedd.indexOf(holdSelection.getItemData().get(0).getItemID());
if (!categoriesAddedd.contains(categoryID)) {
CategoryData data = new CategoryData();
data.setCategoryID(categoryID);
data.setCategoryName(holdSelection.getCategoryName());
data.setItemData(holdSelection.getItemData());
mAddedToCart.add(data);
categoriesAddedd.add(categoryID);
categoriesitemAddedd.add(holdSelection.getItemData().get(0).getItemID());
Toast.makeText(mContext,R.string.Add_to_cart,Toast.LENGTH_SHORT).show();
} else {
if (position>=0) {
if(itemPosition>=0){
if(holdSelection.getItemData().get(0).getScaled()){
Toast.makeText(mContext,R.string.Already_in_cart,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mContext,R.string.Already_in_cart,Toast.LENGTH_SHORT).show();
}
}else{
try{
CategoryData data = mAddedToCart.get(position);
iArr = data.getItemData(); iArr.add(holdSelection.getItemData().get(0));
categoriesAddedd.add(categoryID);
categoriesitemAddedd.add(holdSelection.getItemData().get(0).getItemID());
Toast.makeText(mContext, R.string.Add_to_cart,Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{ // TODO: handle exception Toast.makeText(mContext, "There is " +e,Toast.LENGTH_SHORT).show();
System.out.println("Exception : "+ e.getMessage()); e.printStackTrace();
}
}
}
}
}
}
then my first question will be : what is holdSelection related to?
Please add more parts of your code as your log (asked by Remees).
Alex.
I want to throw an exception in Android so that the app is shut down and a the user gets the ability to report this error to the developer.
I put this into a UI class:
throw new Exception("error...");
but I get an unhandled error. When I use RuntimeException it wont crash and end my application.
You can put in try ... catch a messagebox that ask to you customer to send an email to developer and on the response you can shut down your application.
try {
......
} catch (Exception e) {
dialog.setMessage("Your Message " + e.toString() + ". Send Email?");
dialog.setButtonNegativeText("NO");
dialog.setButtonPositiveText("YES");
dialog.show(getFragmentManager(), "NoticeDialogFragment");
}
Create a user defined exception and throw the exception in the catch block..
MyOwnExceptionClass.java
public class MyOwnExceptionClass extends Exception {
private int price;
public MyOwnExceptionClass(int price){
this.price = price;
}
public String toString(){
return "Price should not be in negative, you are entered" +price;
}
}
your working class
public class Client {
public static void main(String[] args)throws Exception
{
int price = -120;
if(price < 0)
throw new MyOwnExceptionClass(price);
else
System.out.println("Your age is :"+price);
}
}
This might be a complex problem with my application but I'll do my best to describe it as accurately as I can.
I am making an Android Client and making use of a couple of helper classes someone else handed to me at work. The helper Android classes are called TcpClient.java and PVDCAndroidClient.java. PVDCAndroidClient.java makes use out of the TcpCLient, using a tcpCLient object to connect via serverIP and port.
Here is PVDCAndroidClient.java:
public class PVDCAndroidClient {
// constants
public static final String DEFAULT_LOGIN_URI = "http://me.net:8000/";
private TcpClient tcpClient = null;
private UdpClient udpClient = null;
private boolean connected = false;
private boolean loggedin = false;
private static SimpleDateFormat sdf;
private String loginURI = DEFAULT_LOGIN_URI;
private int getUserNumber;
TcpMessageListener listener = null;
/**
* Connects to proxy server, blocks until complete or timeout
* #param serverIP
* #param port
*/
public void connect(String serverIP, int port)
{
try
{
if(serverIP.length() != 0 && port != 0)
{
tcpClient = new TcpClient();
tcpClient.addTcpListener(listener);
tcpClient.connect(serverIP, port);
}
}
catch(Exception e)
{
e.printStackTrace();
Log.d("Could not connect to server, possbile timeout...", "error");
}
}
///// Make login function a blocking call
// Default login, use last location as login location
public boolean login(String fName, String lName, String password)
{
return this.login(fName, lName, password, "last location");
}
public boolean login(String fName, String lName, String password, String region)
{
return this.login(fName, lName, password, "last location", 128, 128, 20);
}
public boolean login(String fName, String lName, String password, String region, int loginX, int loginY, int loginZ)
{ sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
// Check passed values
// x, y and z should be between [0, 256]
// strings should not be null or empty(except lName which can be empty)
if((loginX >= 0 && loginX <= 256) && (loginY >=0 && loginY <= 256) && (loginZ >= 0 && loginZ <= 256))
{
if(fName.length() != 0 && fName != null)
{
// Construct packet xml structure
// Send request and wait until reply or timeout
// return false if timeout (or throw exception?)
// if not timeout, read result packet and determine return value
// getUserNumber = tcpClient.getUserNum();
StringWriter stringWriter = new StringWriter();
XmlSerializer serializer = Xml.newSerializer();
try {
serializer.setOutput(stringWriter);
// Indentation is not required, but helps with reading
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
// TODO: Remove hardcoding of strings, make either constants or place in strings.xml
// TODO: Move the header construction to other method as it is fairly constant other than request num and no need to repeat that much code
serializer.startTag("", "pvdc_pkt");
serializer.startTag("", "pvdc_header");
// TODO: replace this with a string unique to the system
serializer.startTag("", "ID");
serializer.text("838393djdjdjd");
serializer.endTag("", "ID");
// TODO: replace this with actual user number from server
serializer.startTag("", "user_num");
serializer.text("22");//get userNum from above
serializer.endTag("", "user_num");
// TODO: add a request number counter to increment this on each request
serializer.startTag("", "request_num");
serializer.text("1");
serializer.endTag("", "request_num");
serializer.startTag("", "DateTime");
serializer.text(sdf.toString()); //utc time variable.
serializer.endTag("", "DateTime");
serializer.endTag("", "pvdc_header");
serializer.startTag("", "pvdc_content");
serializer.attribute("", "type", "requestlogin");
serializer.startTag("", "name");
serializer.attribute("", "fname", fName);
serializer.attribute("", "lname", lName);
serializer.endTag("", "name");
serializer.startTag("", "password");
serializer.text(password);
serializer.endTag("", "password");
serializer.startTag("", "server");
serializer.text(this.loginURI);
serializer.endTag("", "server");
serializer.startTag("", "location");
serializer.attribute("", "region", region);
serializer.text(loginX + ";" + loginY +";" + loginZ);
serializer.endTag("", "location");
serializer.endTag("", "pvdc_content");
serializer.endTag("", "pvdc_pkt");
// Finish writing
serializer.endDocument();
// write xml data out
serializer.flush();
//
sendLogin(stringWriter);
} catch (Exception e) {
Log.e("Exception", "error occurred while creating xml file");
return false;
}
// Print out xml for debugging
Log.d("PVDCAndroidClient Login", stringWriter.toString().trim());
}
else
{
Log.d("Error in name checking", "fName either blank or null");
}
}
else
{
Log.d("login coordinates X,Y, or Z not between 0-256", "Coordinates Error");
}
return true;
}
// moveString should contain the properly formatted movement command(s)[see above move request description]
public void sendLogin(StringWriter stringWriter)
{
tcpClient.sendMessage(stringWriter.toString());
}
}
Here is the actual TcpClient.java:
public class TcpClient {
public interface TcpMessageListener{
public void onMessage(TcpClient client, String message);
}
private Socket socket = null;
private PrintWriter out = null;
private BufferedReader in = null;
private Thread listenThread = null;
private boolean listening = false;
private int userNum = -1;
private List<TcpMessageListener> listeners = new ArrayList<TcpMessageListener>();
public int getUserNum()
{
return this.userNum;
}
public TcpClient() {
}
public void addTcpListener(TcpMessageListener listener)
{
synchronized(this.listeners)
{
this.listeners.add(listener);
}
}
public void removeTcpListener(TcpMessageListener listener)
{
synchronized(this.listeners)
{
this.listeners.remove(listener);
}
}
public boolean connect(String serverIpOrHost, int port) {
try {
socket = new Socket(serverIpOrHost, port);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.listenThread = new Thread(new Runnable(){
public void run() {
int charsRead = 0;
char[] buff = new char[4096];
while(listening && charsRead >= 0)
{
try {
charsRead = in.read(buff);
if(charsRead > 0)
{
Log.d("TCPClient",new String(buff).trim());
String input = new String(buff).trim();
synchronized(listeners)
{
for(TcpMessageListener l : listeners){
l.onMessage(TcpClient.this, input);
}
}
if (input.toLowerCase().contains("<user_num>")){
int index = input.toLowerCase().indexOf("<user_num>");
index += "<user_num>".length();
int index2 = input.toLowerCase().indexOf("</user_num>");
userNum = Integer.parseInt(input.substring(index, index2));
}
}
} catch (IOException e) {
Log.e("TCPClient", "IOException while reading input stream");
listening = false;
}
}
}
});
this.listening = true;
this.listenThread.setDaemon(true);
this.listenThread.start();
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
return false;
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection");
return false;
} catch (Exception e) {
System.err.println(e.getMessage().toString());
return false;
}
return true;
}
public void sendMessage(String msg) {
if(out != null)
{
out.println(msg);
out.flush();
}
}
public void disconnect() {
try {
if(out != null){
out.close();
out = null;
}
if(in != null){
in.close();
in = null;
}
if (socket != null) {
socket.close();
socket = null;
}
if(this.listenThread != null){
this.listening = false;
this.listenThread.interrupt();
}
this.userNum = -1;
} catch (IOException ioe) {
System.err.println("I/O error in closing connection.");
}
}
}
LASTLY, here is what I have been coding today and cannot seem to get this to work. I don't get any blatant exceptions, just a warning on Logcat, that says, "Couldn't get I/O for the connection".
public class AndroidClientCompnt extends Activity {
private TcpClient myTcpClient = null;
private UdpClient udpClient;
private static final String IP_ADDRESS_SHARED_PREFS = "ipAddressPref";
private static final String PORT_SHARED_PREFS = "portNumberPref";
private String encryptPassLoginActivity;
private String getIpAddressSharedPrefs;
private String getPassword, getName, getRegionSelect, getGridSelect;
private String fName, lName;
private SharedPreferences settings;
private boolean resultCheck = false;
private int portNum;
PVDCAndroidClient client;
private String name;
private CharSequence[] getView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
Intent intent = getIntent();
// getting object's properties from LoginActivity class.
getName = intent.getStringExtra("name");
getPassword = intent.getStringExtra("password");
getRegionSelect = intent.getStringExtra("regionSelect");
getGridSelect = intent.getStringExtra("gridSelect");
Log.d("VARIABLES", "getName = " + getName + "getPassword" + getPassword
+ "getRegionSelect = " + getRegionSelect + ".");
setResult(Activity.RESULT_CANCELED);
client = new PVDCAndroidClient();
}
#Override
protected void onStart() {
super.onStart();
// Take care of getting user's login information:
// grid selected as well? sometime?
settings = PreferenceManager.getDefaultSharedPreferences(this);
getIpAddressSharedPrefs = settings.getString(IP_ADDRESS_SHARED_PREFS,
"");
portNum = Integer.parseInt(settings.getString(PORT_SHARED_PREFS, ""));
Log.d("SHARED" + getIpAddressSharedPrefs + "port " + portNum, "");
if (getIpAddressSharedPrefs.length() != 0 && portNum != 0) {
try {
// first connect attempt.
client.connect(getIpAddressSharedPrefs, portNum);
resultCheck = client.isConnected();
// here is where I want to call Async to do login
// or do whatever else.
UploadTask task = new UploadTask();
task.execute();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Could not connect.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(),
"Ip preference and port blank", Toast.LENGTH_LONG).show();
}
finish();
}
private class UploadTask extends AsyncTask<String, Integer, Void> {
#Override
protected void onPreExecute() {
Toast.makeText(getApplicationContext(), "Loading...",
Toast.LENGTH_LONG).show();
}
#Override
protected Void doInBackground(String... names) {
// encrypting user's password with Md5Hash class.
try {
encryptPassLoginActivity = MdHashing
.MD5(getPassword.toString());
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (resultCheck == true) {
String[] firstAndLast;
String spcDelmt = " ";
firstAndLast = name.toString().split(spcDelmt);
fName = firstAndLast[0];
lName = firstAndLast[1];
// set up the tcp client to sent the information to the
// server.
client.login(fName, lName, encryptPassLoginActivity,
getRegionSelect, 128, 128, 20);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
Intent goToInWorld = new Intent(
AndroidClientCompnt.this.getApplicationContext(),
PocketVDCActivity.class);
startActivity(goToInWorld);
Toast.makeText(getApplicationContext(), "Connected",
Toast.LENGTH_LONG).show();
}
}
}
I know this is a super long post and I am asking a lot but if anyone could take a look at this I would very much appreciate it. I've been at this all day, trying to make use of these helper classes I got and can't get it to work. It also doesn't help that I'm not too experienced in this client/server stuff. Any nudges in the right direction or an accepted solution would REALLY mean something to me.
Thank you kindly,
Have a good evening.
Can you post your manifest?
You may need to add the following :
<uses-permission android:name="android.permission.INTERNET"/>
Additionally - I assume you see nothing ever happen on the server side of this connection?
1) Make sure you have the following permission in your Android-Manifest file:
<uses-permission android:name="android.permission.INTERNET/>
w/o this you definitely won't be making any tcp/ip connections.
2) You will want to run the code in debug mode, and place breakpoints where the connection information
is set and also what results are at several points. In other words you need to dig deeper.
If you are somewhat new to coding there is no better investment of time than in running the debugger and stepping line by line through the code. Code only comes to life inside a debugger, where you can see the values of variables and results. So set several breakpoints, step through and you will see more. It is more difficult to debug where there are threads however.