首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

上网错误

2013-12-11 
上网异常我的上网程序出现以下异常,请问是什么原因?程序代码如下:private boolean createHttp(){String ht

上网异常
我的上网程序出现以下异常,请问是什么原因?
上网错误

程序代码如下:


    private boolean createHttp(){
    String httpUrl = "http://www.google.com.hk/";    
    try{
    // 取得HttpClient对象
    HttpClient httpClient = new DefaultHttpClient();
    Log.i("DEBUG", "OK1");
    // 取得HttpGet对象
        HttpGet httpGet = new HttpGet(httpUrl);    
    Log.i("DEBUG", "OK2");
    // 请求HttpClient,取得HttpResponse
    HttpResponse httpResponse = httpClient.execute(httpGet);
    Log.i("DEBUG", "OK3");
    // 请求成功
    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
    {
    // 取得返回的字符串
    String strResult = EntityUtils.toString(httpResponse.getEntity());
    tv1.setText(strResult);
    return true;
    }
    else
    {
    tv1.setText("请求错误!");
    }
    } catch (ClientProtocolException e){
    Log.i("DEBUG", "失败1");
    tv1.setText(e.getMessage());
    } catch (IOException e) {
    Log.i("DEBUG", "失败2");
    tv1.setText(e.getMessage());
    } catch (Exception e) {
    Log.i("DEBUG", "失败3");
tv1.setText(e.getMessage());
            e.printStackTrace();
    }   
    return false;
    }


[解决办法]
HTTP请求要放入子线程中

热点排行