阻止HTTP调用似乎超时

时间:2015-03-30 13:01:14

标签: android http blocking

我想对我的php文件发出阻塞调用,该文件查询我的数据库。我希望它是阻止而不是异步,因为我不希望用户在此呼叫成功之前显示任何内容。

我运行以下代码,它在response = httpclient.execute(httpget);停留一两分钟,然后进入catch for error。我看不到e项中的错误。

有人知道这里发生了什么 - 我的php文件适用于异步任务。

public static String blockingHttpCallToUrl(String url)
{
    String result="nothingReturned";
    HttpClient httpclient = new DefaultHttpClient();

    // Prepare a request object
    HttpGet httpget = new HttpGet(url);

    // Execute the request
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
        // Examine the response status
        Log.i("Praeda", response.getStatusLine().toString());

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        // If the response does not enclose an entity, there is no need
        // to worry about connection release

        if (entity != null) {
            // A Simple JSON Response Read
            InputStream instream = entity.getContent();
            result= convertStreamToString(instream);
            // now you have the string representation of the HTML request
            instream.close();
        }


    } catch (Exception e) {}

    return result;
}

0 个答案:

没有答案