android volley 在没有互联网连接的情况下崩溃

标签 android crash try-catch send android-volley

我有问题。我花了一段时间才弄清楚 Volley 甚至更好,当它尝试在没有连接的情况下发送数据(2G/3G/LTE,wifi)时,完整的应用程序崩溃了。如果智能手机处于飞行模式甚至没有互联网连接,就会发生这种情况。

这是我的源代码:

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

}

这就是我的应用程序中请求的样子。我要发送一个 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);
}

如果智能手机有互联网连接(2G/3G/LTE、wifi),一切正常。如果它处于飞行模式,我会收到此错误:

enter image description here

有人可以帮我处理这个吗?我什至用“try”和“catch” block 尝试过,但这对我不起作用。 Volley 可以在不崩溃应用程序的情况下管理这个吗?提前致谢! :)

最佳答案

我认为最有可能是因为这条线而抛出 NPE:

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

“错误”对象可能为空。

关于android volley 在没有互联网连接的情况下崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26496229/

相关文章:

android - 如何在android中启用/禁用飞行模式?

ios - 崩溃 firebase Swift

javascript - TypeError : node. setAttribute 不是函数

android - com.android.dex.DexException : Multiple dex files define Landroid/support/annotation/AnimRes;

android - 将数据发布到服务器并在服务器端读取

android - 如何在圆角布局上添加波纹效果?

java - 应用程序从 URL 检索图像并在 Android 应用程序中显示时崩溃

在没有 Root 访问的情况下,Linux 在 CPL3(用户模式)下会崩溃或挂起吗?

scala - 在 Scala 中将 List[Try[A]] 转换为 List[A]

java - 如何正确提取在java中加载资源的简单try/catch block ?