ssummeraw 2013-06-09 03:04 采纳率: 10%
浏览 2232
已采纳

从 android 应用程序响应时间发送 php 查询到服务器

我想在数据库中放入一个entry。用的下面的代码:

String url = String.format("http://xxxxxxx/xxx/index.php?home=yes&pid=%s", pid);
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
    try {
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler);
         Log.e("abc", response);
        return response;
        } catch (ClientProtocolException es) 
        {   
            Log.e("x" , es.getMessage());
            return "";
        } catch (IOException e) 
        {
            Log.e("aasx" , e.getMessage());
            return "";
        }

这段代码能正常运行,现在当有网络连接的时候,代码就会困在httpclient.execute(httppost, responseHandler); 大约俩分钟。

有什么方法可以实现检查几秒钟后,如果没有反应的就会就catch the exceptions?

  • 写回答

1条回答 默认 最新

  • JaveZh 2013-06-09 04:04
    关注
       HttpParams params =new BasicHttpParams();
       // 设置一些基本参数
       HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
       HttpProtocolParams.setContentCharset(params,CHARSET);
       HttpProtocolParams.setUseExpectContinue(params, true);
       /* 从连接池中取连接的超时时间 */
       ConnManagerParams.setTimeout(params, 1000);
       /* 连接超时 */
       HttpConnectionParams.setConnectionTimeout(params, 2000);
       /* 请求超时 */
       HttpConnectionParams.setSoTimeout(params, 4000);
       // 设置我们的HttpClient支持HTTP和HTTPS两种模式
       SchemeRegistry schReg =new SchemeRegistry();
       schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            schReg.register(new Scheme("https", SSLSocketFactory
                    .getSocketFactory(), 443));
    
       // 使用线程安全的连接管理来创建HttpClient
       ClientConnectionManager conMgr =new ThreadSafeClientConnManager(params, schReg);
       HttpClient httpclient=new DefaultHttpClient(conMgr, params);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站