Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中显示web调用超时异常警报_Android - Fatal编程技术网

如何在android中显示web调用超时异常警报

如何在android中显示web调用超时异常警报,android,Android,我需要显示Http web调用超时异常的警报消息。(即)我需要在android中显示特定时间段(如5秒)的web调用的警报,如“服务器连接超时”。我该怎么做 我的代码: 公共类MainActivity扩展了活动{ static InputStream is = null; static JSONObject jObj = null; static HttpClient httpClient; String json, Token; private ProgressDialog progressD

我需要显示Http web调用超时异常的警报消息。(即)我需要在android中显示特定时间段(如5秒)的web调用的警报,如“服务器连接超时”。我该怎么做

我的代码:

公共类MainActivity扩展了活动{

static InputStream is = null;
static JSONObject jObj = null;
static HttpClient httpClient;
String json, Token;
private ProgressDialog progressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    progressDialog = ProgressDialog.show(MainActivity .this, "", "Loading...",
            true);
    new Thread() {
        // Run Mehod
        @Override
        public void run() {
            getCC();
            messagehandler.sendEmptyMessage(0);
        } // End of run
    }.start();
}

private Handler messagehandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == 0) {
            progressDialog.dismiss();
            String response=json;
            //Do steps from response
        }

    }

};

private void getCC() {
    SSLCertificate();
    // HttpClient client = new DefaultHttpClient();
    String getURL = "My URL";
    HttpGet get = new HttpGet(getURL);

    get.setHeader("Accept", "*/*,text/xml,application/json");
    get.setHeader("Cache-Control", "no-cache");
    get.setHeader("Content-type", "application/json");

    HttpResponse response;
    try {
        // defaultHttpClient

        response = httpClient.execute(get);
        HttpEntity httpEntity = response.getEntity();
        is = httpEntity.getContent();

    }catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {

    }

}

private static void SSLCertificate() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setContentCharset(params, "HTTP.ISO-8859-1");
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(),
            443));

    ClientConnectionManager ccm = new ThreadSafeClientConnManager(params,
            schemeRegistry);
    HttpConnectionParams.setConnectionTimeout(params, 300);

    HttpConnectionParams.setSoTimeout(params, 300);

    httpClient = new DefaultHttpClient(params);

    httpClient = new DefaultHttpClient(ccm, params);

}
} 谢谢


如果花费的时间超过10000毫秒,将引发异常使用
HttpParams

int timeout = (int) (5 * DateUtils.SECOND_IN_MILLIS);
params.setConnectionTimeout(params, timeout);
params.setSoTimeout(params, timeout);

然后,用
try-catch

环绕,如何处理catch stmt?我需要在哪里放置警报?修改代码以包括上面的参数,用
try/catch
环绕,将警报代码放在
catch
块中。无法100%确定它引发了什么异常。我认为是
IOException
ConnectTimeoutException
SocketTimeoutException
-谷歌it:)
int timeout = (int) (5 * DateUtils.SECOND_IN_MILLIS);
params.setConnectionTimeout(params, timeout);
params.setSoTimeout(params, timeout);