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 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流