【问题标题】:android volley crashes without internet connectionandroid volley 在没有互联网连接的情况下崩溃
【发布时间】:2014-10-21 21:00:56
【问题描述】:

我有一个问题。我花了一段时间才弄清楚凌空甚至更好,当它尝试在没有连接的情况下发送数据(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),一切正常。如果它处于飞行模式,我会收到此错误:

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

【问题讨论】:

    标签: android crash try-catch send android-volley


    【解决方案1】:

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

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

    “错误”对象可能为空。

    【讨论】:

    • 嗯......这有点尴尬,但它似乎正在工作:) 我必须验证这一点才能确定。我会将其标记为已回答迈克! :) 非常感谢您指出这一点! :)
    • 没问题! :) 祝你好运!
    • 抱歉这么晚才发布 :) 这就是它崩溃的原因 :) 你还有通过 volley 发送 jsonarray 的解决方案吗? :)
    • 这真的取决于服务器期望如何接收该数组。你有这方面的一些信息吗?
    • 好吧,我只是在这里使用 raw_http_data:“$obj = json_decode($HTTP_RAW_POST_DATA);”这是我要解码的简单 json 对象的代码和平。到目前为止,我还找不到一个带有 JSONArray 的凌空工作示例:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多