Bertho Joris
Bertho Joris

Reputation: 1601

How do I catch error when access server

I create my util to request data from the server :

public class PushServer {
      public static ArrayList<DataBean> getAllData() {
        ArrayList<DataBean> data = new ArrayList<DataBean>();

        String uriJson = "http://api.mydomain.com/news.html";
        String resp = Utils.getResponseText1(uriJson);

        try {
            JSONObject json = new JSONObject(resp);
            JSONArray jArray = json.getJSONArray("items");

            for (int i=0; i<jArray.length(); i++) {
                JSONObject jData = jArray.getJSONObject(i);

                DataBean bean = new DataBean();             
                bean.setMenu_ID(jData.getString("ID"));
                bean.setIsDisplay(jData.getString("First_Data"));
                bean.setCategoryName(jData.getString("Second_Data"));

                data.add(bean);
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return data;
    }
 }

When i call my util in AsyncTask :

protected void onPreExecute() {
    super.onPreExecute();
    pgBar.setVisibility(View.VISIBLE);
}

protected Void doInBackground(Void... params) {
    try {
        databean = PushServer.getAllData();
    } catch (Exception e) {
        netError.setVisibility(View.VISIBLE);
    }
    return null;
}

I don't get an error response. My screen is blank when my connection is down... How do I make a response if the URL API can not be accessed by my android application, such as the Troubled connection, etc.

Upvotes: 1

Views: 51

Answers (2)

Phil
Phil

Reputation: 36289

You can use the droidQuery library to simplify this and provide error handling:

final ArrayList<DataBean> data = new ArrayList<DataBean>();
$.ajax(new AjaxOptions().url("http://api.mydomain.com/news.html").success(new Function() {
    @Override
    public void invoke($ d, Object... args) {
        JSONObject json = (JSONObject) args[0];
         JSONArray jArray = json.getJSONArray("items");
         for (int i=0; i<jArray.length(); i++) {
             JSONObject jData = jArray.getJSONObject(i);
              DataBean bean = new DataBean();             
              bean.setMenu_ID(jData.getString("ID"));
              bean.setIsDisplay(jData.getString("First_Data"));
              bean.setCategoryName(jData.getString("Second_Data"));
              data.add(bean);

              //hear you may want to make a call to handle the "next step" for how to process this bean.
         }
     }
}).error(new Function() {
    @Override
    public void invoke($ d, Object... args) {
        AjaxError error = (AjaxError) args[0];
        //print the error status code and reason to logcat
        Log.e("Ajax", "Error " + error.status + ": " + error.reason);
    }
}));

Upvotes: 1

Snicolas
Snicolas

Reputation: 38168

There are a few issues in your code. Most are not that important, but conceptually, there is a big one : you should not use an asynctask to access the network on android.

The flaws of AsyncTasks are explained in this thread.

Moreover, when it comes to your problem of catching an exception the right way, AsyncTasks are even worse : there is no error handling built-in. You could still have some kind of manual error handling implemented (retain the exception, and make it accessible via a getter, then ask if any exception was received when your asynctasks is done).

But more and more libs offer much more interesting alternatives when it comes to the network, and most of them propose a nice error handling model for async exceptions. Have a look at the thread I mentionned, RoboSpice is among the alternatives. And if you don't like it, it also lists all other well known alternatives at the end of the readme on github.

Upvotes: 2

Related Questions