如何解决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;
}
我得到了例外,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否检查了清单文件的互联网权限?
did you check for the manifest file for Internet permission?
目前还不知道网络异常,但最后一个 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 throughhttpclient.execute(delete)
.You should rewrite your
catch
block and write the name of the catched exception and the message to the log.在第 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