如何解决android中的异常

发布于 2024-11-09 17:43:42 字数 1618 浏览 0 评论 0原文

我已经在 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;
        }

我得到了例外,

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

有人知道为什么吗?

i have written HttpDelete in android to call REST web service.

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;
        }

And I get the exception which is

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

Do anyone know why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[浮城] 2024-11-16 17:43:42

您是否检查了清单文件的互联网权限?

did you check for the manifest file for Internet permission?

本王不退位尔等都是臣 2024-11-16 17:43:42

目前还不知道网络异常,但最后一个 NPE 很可能是因为前一个异常是在通过 httpclient.execute(delete) 初始化 response 之前引发的。

您应该重写您的catch 块,并将捕获的异常的名称和消息写入日志。

No idea for the network exception (yet) but the last NPE is most likely because the previous exception is thrown before response has been initialized through httpclient.execute(delete).

You should rewrite your catch block and write the name of the catched exception and the message to the log.

独闯女儿国 2024-11-16 17:43:42

在第 4 行的 onCreate() 函数中,我们发现在初始化 TextView 后,您尝试设置一个值。

您必须使用 Handler 对象,因为您无法在单独的线程中更新 UI 对象。

这是一些帮助链接

https://web.archive.org/web/20200810154212/http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html

https://web.archive.org/web/20130306131310 /http://thedevelopersinfo.com/?p=150

In the onCreate() function on line 4 we find that after initializing your TextView you are trying to set a value.

You must use a Handler object because you cannot update an UI objects while in a separate thread.

Here is some link for your help

https://web.archive.org/web/20200810154212/http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html

https://web.archive.org/web/20130306131310/http://thedevelopersinfo.com/?p=150

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文