简体   繁体   中英

android volley crashes without internet connection

I ve got a problem. It took me a while to figure it out that volley or even better, the complete app crashes when It tries to send data with no connection (2G/3G/LTE,wifi). This can happen if the smartphone is in flight mode or even got no internet connection.

Here is my source code:

public class AppController extends Application {

public static final String TAG = AppController.class
        .getSimpleName();

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    getRequestQueue();
    if (mImageLoader == null) {
        mImageLoader = new ImageLoader(this.mRequestQueue,
                new LruBitmapCache());
    }
    return this.mImageLoader;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    // set the default tag if tag is empty
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}


public void addToRequestQueue(JsonObjectRequest jsonObjReq,
        String tag_json_obj) {
    // TODO Auto-generated method stub
    getRequestQueue().add(jsonObjReq);
}   

}

This is how a requests looks like within my app. I am going to send an JSONObject.

public static void sendSMS(JSONObject obj)
{
// Tag used to cancel the request
String tag_json_obj = "json_obj_req";

String url = "http:/<IP>/<FILE>.php";


        JsonObjectRequest ObjReq = new JsonObjectRequest(Method.POST, url, obj,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, response.toString());
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        // hide the progress dialog
                    }
                });

// Adding request to request queue
AppController.getInstance().addToRequestQueue(ObjReq, tag_json_obj);
}

If the smartphone gots an internet connection (2G/3G/LTE,wifi), everything is working fine. If it is in flight mode I get this error:

在此处输入图片说明

Can someone please help me to handle this? I even tryed it with "try" and "catch" blocks but this isn´t working for me. Can volley manage this without crashing the app? Thanks in advance! :)

I think most probably is throwing NPE becuase of this line:

VolleyLog.d(TAG, "Error: " + error.getMessage());

The "error" object might be null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM