zzz125x 2015-04-30 13:44 采纳率: 0%
浏览 3508

android http连接问题。编译不报错,真机运行闪退

public class MainActivity extends Activity {
private static String url="www.baidu.com";
private TextView a1 = null;
private HttpResponse httpResponse=null;

private HttpEntity httpEntity=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a1 = (TextView) findViewById(R.id.textView);
getPDAServerData(url);
}

   /**
    * 请求服务
    * @param url
    */
   private void getPDAServerData(String url){
     // TODO Auto-generated method stub  
        //生成一个请求对象  
        HttpGet httpGet=new HttpGet("http://www.baidu.com");  
        //生成一个Http客户端对象  
        HttpClient httpClient=new DefaultHttpClient();  
        //使用Http客户端发送请求对象  
        InputStream inputStream=null;  
        try {  
            httpResponse=httpClient.execute(httpGet);  
            //收到服务器的响应之后把返回的数据读取出来  
            httpEntity=httpResponse.getEntity();  
            inputStream=httpEntity.getContent();  
            //流文件的读取  
            BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));  
            String resultString="";  
            String lineString="";  
            while((lineString=reader.readLine())!=null){  
                resultString=resultString+lineString;  
            }  
            a1.setText(resultString);  
        } catch (ClientProtocolException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        finally{  
            try {  
                inputStream.close();  
            } catch (Exception e2) {  
                // TODO: handle exception  
                e2.printStackTrace();  
            }  

}
}
}
错误日志
图片说明

  • 写回答

8条回答 默认 最新

  • 扯线木偶人 2015-04-30 14:21
    关注

    抛出的异常名称是什么?
    检查一下MainActivity.java 第45行

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条