【问题标题】:Android -Json httpRequest Time out ConnectionAndroid -Json http请求超时连接
【发布时间】:2013-03-24 05:42:35
【问题描述】:

当我执行 httpRequest 时,如何设置连接超时?我回到之前的活动?因为我的 apilcacion 被防火墙阻止了,我可以腾出一些时间无法连接到服务器,我返回到上一个活动并显示一个 Toast 告诉我没有互联网连接?

这是我的代码的一部分:

protected String doInBackground(String... args) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        JSONObject json = jParser.makeHttpRequest(url_list, "GET",
                params);

        Log.d("All Products: ", json.toString());

        try {

            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {

                daftar_rs = json.getJSONArray(TAG_DAFTAR_RS);

                for (int i = 0; i < daftar_rs.length(); i++) {
                    JSONObject c = daftar_rs.getJSONObject(i); 
//Continue here with a process to show a listview...

当没有互联网连接时,应用程序完全关闭并显示错误,有人知道我该如何解决吗?非常感谢!老师们好!

【问题讨论】:

  • 请看我的回答,如果您想使用HttpClient 来检索json字符串,我可以发布更多示例代码
  • @lelloman 我想使用 HttpClient,如果连接被拒绝,我想返回上一个活动并显示 Toast“连接失败”
  • @lelloman 我认为这更像是我使用的 ConnectException,因为如果没有可用的互联网,但如果应用程序无法连接到互联网,例如被防火墙阻止
  • gulp...所以您不需要检查连接性,顺便说一句,有一天可能会派上用场...请查看编辑

标签: android json connection timeout httprequest


【解决方案1】:

要检查您是否可以访问互联网,您可以使用类似

public boolean connesso() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

对于连接超时,我使用 httpclient 来发出这样的请求,您也可以处理套接字超时

HttpParams httpParameters = new BasicHttpParams();
int timeout1 = 1000*10;
int timeout2 = 1000*10;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeout1);
HttpConnectionParams.setSoTimeout(httpParameters, timeout2);
HttpClient httpclient = new DefaultHttpClient(httpParameters);

String jsonString = "";
HttpGet request;
try {
    request = new HttpGet(new URI(url));

    request.addHeader("User-Agent", "Android");

    HttpResponse response = httpclient.execute(request);

    StatusLine statusLine = response.getStatusLine();
    if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);
        out.close();
        jsonString = out.toString();
    }

    }catch(ConnectException e){             
    // handle your exception here, maybe something like
        Toast.makeText(context,"Error!",5000).show();
        finish(); // if your are within activity class, otherwise call finish on your activity          

    } catch (URISyntaxException e1) {
    // TODO Auto-generated catch block
        e1.printStackTrace();
        } catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
JSONObject myJsonObject = new JSONObject(jsonString);
// etc...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多