android print"连接超时"在loopj中超时

时间:2016-04-13 05:56:40

标签: android loopj

android我发现更多但没有得到解决方案..

当在loopj中发生超时时,我想要打印超时消息..

按照我使用的超时代码。 private static final int DEFAULT_TIMEOUT = 15 * 1000;

private static AsyncHttpClient client = new AsyncHttpClient();

public static void setTimeOutTime() {

    client.setTimeout(DEFAULT_TIMEOUT);
    System.out.println("timeout");
}

1 个答案:

答案 0 :(得分:0)

    try {
        response = client.newCall(request).execute();
        jsonObj = new JSONObject(response.body().string());
    } catch (IOException e) {
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (getStatus() == Status.RUNNING) {
                    cancel(true);
                    if(!hiddenDialog) {
                        if (pDialog.isShowing())
                            pDialog.dismiss();
                    }
                }

                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                builder.setTitle("Timeout error")
                        .setMessage("Sorry server doesn't response!\nCheck your internet connection and try again.")
                        .setCancelable(true)
                        .setPositiveButton("Try again", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                Log.d(TAG, "load again");
                            }
                        }).setIcon(R.drawable.ic_dialog_alert_dark);
                AlertDialog alert = builder.create();
                alert.show();
            }
        });
    } catch (JSONException e) {}

修改 对于你的情况,它应该是相同的方式

mActivity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
      // make toast
    }
});