使用 HttpResponse 时出现异常 response = client.execute(request);

发布于 2024-11-19 20:22:44 字数 1379 浏览 2 评论 0原文

我试图从服务器请求响应,但是当我使用“HttpResponse response = client.execute(request);”时,程序进入异常情况。

这是我的代码:

用于从服务器获取响应的函数

public StringexecuteHttpGet(String username, String password) throws Exception {

    BufferedReader in = null;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI("http://emapzoom.com/setting/device_login"+ "?device_id=" +password+ "&login_name="+ username));



        HttpResponse response = client.execute(request);
        in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        String page = sb.toString();
        System.out.println(page);
        return page;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

当我执行时在活动中使用的代码

     try{

            test=executeHttpGet(name,pass);

        }catch(Exception e){

        }

,程序进入 catch 块!

请帮我 !!! 提前谢谢!

I'm trying to request the response from server, but when I use "HttpResponse response = client.execute(request);", program enters the exception case.

Here is my code:

function for getting response from server

public String executeHttpGet(String username, String password) throws Exception {

    BufferedReader in = null;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI("http://emapzoom.com/setting/device_login"+ "?device_id=" +password+ "&login_name="+ username));



        HttpResponse response = client.execute(request);
        in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        String page = sb.toString();
        System.out.println(page);
        return page;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

code used in activity

     try{

            test=executeHttpGet(name,pass);

        }catch(Exception e){

        }

when I execute, program enter the catch block!

please help me !!!
thx in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶花眉 2024-11-26 20:22:45

如果您正在针对任何版本的 Android >= Honeycomb 进行构建,则无法在主线程上进行网络调用。尝试将其放入异步任务中,看看它是否有效。

If you're building against any version of Android >= Honeycomb you cannot make network calls on the main thread. Try putting this in an Async Task and see if it works.

笔落惊风雨 2024-11-26 20:22:45

dell116的回答是正确的。

我在 ICS 上遇到了同样的问题,并使用以下代码异步解决了它:

private void getResponseThread(final String url) {
    new Thread(new Runnable() {
        public void run() {
            String cadHTTP = getResponse(url);
            Message msg = new Message();
            msg.obj = cadHTTP;
            handlerHTTP.sendMessage(msg);
        }
    }).start();
}

private String getResponse(String url) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet del = new HttpGet(url);
    del.setHeader("content-type", "application/json");

    String respStr;
    try {
        HttpResponse resp = httpClient.execute(del);
        respStr = EntityUtils.toString(resp.getEntity());
    } catch(Exception ex) {
        Log.e("RestService","Error!", ex);
        respStr = "";
    }

    Log.e("getResponse",respStr);
    return respStr;
}

private Handler handlerHTTP = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        String res = (String) msg.obj;
        //CONTINUE HERE
        nexTask(res);
    }
};

问候! :)

The answer of dell116 are right.

I had the same problem on ICS and solve it asynchronously with this code:

private void getResponseThread(final String url) {
    new Thread(new Runnable() {
        public void run() {
            String cadHTTP = getResponse(url);
            Message msg = new Message();
            msg.obj = cadHTTP;
            handlerHTTP.sendMessage(msg);
        }
    }).start();
}

private String getResponse(String url) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet del = new HttpGet(url);
    del.setHeader("content-type", "application/json");

    String respStr;
    try {
        HttpResponse resp = httpClient.execute(del);
        respStr = EntityUtils.toString(resp.getEntity());
    } catch(Exception ex) {
        Log.e("RestService","Error!", ex);
        respStr = "";
    }

    Log.e("getResponse",respStr);
    return respStr;
}

private Handler handlerHTTP = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        String res = (String) msg.obj;
        //CONTINUE HERE
        nexTask(res);
    }
};

Regards! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文