2 hcg 21 hcg_21 于 2013.01.31 16:02 提问

如何解决 android 中的异常问题?

在Android程序中,我创建了HttpDelete方法来调用REST web服务。

public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView txt = (TextView) findViewById(R.id.textView1);
    txt.setText(getInputStreamFromUrl("http://192.168.37.241:8080/kyaw/k"));
    }

    public static String getInputStreamFromUrl(String url) {
          InputStream content = null;
          HttpResponse response = null;
          try {

            DefaultHttpClient httpclient = new DefaultHttpClient();             
            HttpDelete delete=new HttpDelete(url);
            put.setHeader("Content-Type","application/vnd.org.snia.cdmi.container");
            response = httpclient.execute(delete);
            content = response.getEntity().getContent();
          }catch (Exception e) {
            Log.e("[DELETE REQUEST]", "Network exception");
          }
            String result=response.getStatusLine().toString()+"\n"+response.getHeaders(url);
            return result;
        }

但是获得以下异常

868: ERROR/[DELETE REQUEST](1197): Network exception
01-23 08:30:16.868: DEBUG/AndroidRuntime(1197): Shutting down VM
01-23 08:30:16.878: WARN/dalvikvm(1197): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-23 08:30:16.908: ERROR/AndroidRuntime(1197): FATAL EXCEPTION: main
01-23 08:30:16.908: ERROR/AndroidRuntime(1197): java.lang.RuntimeException: Unable to start activity ComponentInfo

谁能帮忙解释一下呢?

3个回答

cytown
cytown   2013.01.31 16:31
已采纳
try {

            DefaultHttpClient httpclient = new DefaultHttpClient();             
            HttpDelete delete=new HttpDelete(url);
            put.setHeader("Content-Type","application/vnd.org.snia.cdmi.container");
            response = httpclient.execute(delete);
            content = response.getEntity().getContent();
            String result=response.getStatusLine().toString()+"\n"+response.getHeaders(url);
            return result;
          }catch (Exception e) {
            Log.e("[DELETE REQUEST]", "Network exception");
          }
          return "";
        }
fandyvon
fandyvon   Rxr 2013.01.31 17:21

网络操作最好不要放在主线程中

clcurie_11
clcurie_11   2013.01.31 17:44

你检查 manifest 文件的网络许可了吗?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!