我正在尝试做xml解析程序。xml页面的链接是:http ://api.androidhive.info/pizza/?format=xml
在进行 http 连接时,它显示错误:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xml/com.example.xml.XmlparseActivity}: java.lang.NullPointerException
然后我将日志消息放在每一行中,所以问题出在 HttpResponce 在该行之后没有显示日志消息。解决问题。任何人都可以帮助我的程序代码如下所示:
public String getXmlElement(String url) {
// TODO Auto-generated method stub
String xml=null;
try{
DefaultHttpClient client=new DefaultHttpClient();
Log.d("cli", "ok");
HttpPost post= new HttpPost(url);
Log.d("post", "ok"+post);
HttpResponse responce=client.execute(post);
Log.d("responce", "ok");
HttpEntity entity=responce.getEntity();
Log.d("entity", "ok");
xml=EntityUtils.toString(entity);
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
return xml;
}
logcat 页面是:
07-05 17:13:03.227: D/hai(809): http://api.androidhive.info/pizza/?format=xml
07-05 17:13:03.237: D/cli(809): ok
07-05 17:13:03.247: D/post(809): okorg.apache.http.client.methods.HttpPost@44ee99e8
07-05 17:13:03.317: W/System.err(809): java.net.UnknownHostException: api.androidhive.info
07-05 17:13:03.317: W/System.err(809): at java.net.InetAddress.lookupHostByName(InetAddress.java:513)
07-05 17:13:03.327: W/System.err(809): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:278)
07-05 17:13:03.327: W/System.err(809): at java.net.InetAddress.getAllByName(InetAddress.java:242)
07-05 17:13:03.327: W/System.err(809): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
07-05 17:13:03.337: W/System.err(809): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-05 17:13:03.337: W/System.err(809): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-05 17:13:03.337: W/System.err(809): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
07-05 17:13:03.347: W/System.err(809): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-05 17:13:03.347: W/System.err(809): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-05 17:13:03.347: W/System.err(809): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-05 17:13:03.357: W/System.err(809): at com.example.xml.XmlParser.getXmlElement(XmlParser.java:93)
07-05 17:13:03.357: W/System.err(809): at com.example.xml.XmlparseActivity.onCreate(XmlparseActivity.java:33)
07-05 17:13:03.357: W/System.err(809): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-05 17:13:03.357: W/System.err(809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-05 17:13:03.357: W/System.err(809): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-05 17:13:03.367: W/System.err(809): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-05 17:13:03.367: W/System.err(809): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-05 17:13:03.367: W/System.err(809): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 17:13:03.367: W/System.err(809): at android.os.Looper.loop(Looper.java:123)
07-05 17:13:03.377: W/System.err(809): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-05 17:13:03.377: W/System.err(809): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 17:13:03.377: W/System.err(809): at java.lang.reflect.Method.invoke(Method.java:521)
07-05 17:13:03.377: W/System.err(809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-05 17:13:03.388: W/System.err(809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-05 17:13:03.388: W/System.err(809): at dalvik.system.NativeStart.main(Native Method)
07-05 17:13:03.407: D/AndroidRuntime(809): Shutting down VM
07-05 17:13:03.407: W/dalvikvm(809): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-05 17:13:03.427: E/AndroidRuntime(809): FATAL EXCEPTION: main
07-05 17:13:03.427: E/AndroidRuntime(809): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xml/com.example.xml.XmlparseActivity}: java.lang.NullPointerException
07-05 17:13:03.427: E/AndroidRuntime(809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-05 17:13:03.427: E/AndroidRuntime(809): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-05 17:13:03.427: E/AndroidRuntime(809): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-05 17:13:03.427: E/AndroidRuntime(809): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-05 17:13:03.427: E/AndroidRuntime(809): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 17:13:03.427: E/AndroidRuntime(809): at android.os.Looper.loop(Looper.java:123)
07-05 17:13:03.427: E/AndroidRuntime(809): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-05 17:13:03.427: E/AndroidRuntime(809): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 17:13:03.427: E/AndroidRuntime(809): at java.lang.reflect.Method.invoke(Method.java:521)
07-05 17:13:03.427: E/AndroidRuntime(809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-05 17:13:03.427: E/AndroidRuntime(809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)