本文介绍了获得一个例外,而使用的Htt presponse响应= client.execute(请求);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从服务器请求的响应,但是当我用的Htt presponse响应= client.execute(请求);程序进入异常情况

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

下面是我的code:

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

function for getting response from server

公共字符串executeHttpGet(用户名字符串,字符串密码)抛出异常{

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

code used in activity

     try{

            test=executeHttpGet(name,pass);

        }catch(Exception e){

        }

当我执行,程序进入catch块!

when I execute, program enter the catch block!

请帮帮我!
THX提前!

please help me !!!thx in advance!

推荐答案

如果您正在构建针对Android的> =蜂窝的任何版本你不能在主线程网络调用。尝试把这个在一个异步任务,看看它是否工作。

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.

这篇关于获得一个例外,而使用的Htt presponse响应= client.execute(请求);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:16