0

我正在尝试做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)
4

3 回答 3

1
public String getXmlElement(String url) {

    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();
    }
catch(NullPointerException e){
   Toast.makeText(getApplicationContext(), "Connection to server failed.. Please try later..", Toast.LENGTH_LONG).show();
}
    return xml;

}

您可以使用如上所示的 catch向用户显示他可能未连接到 Internet。空指针异常可能发生在任何一行,因此并不准确。

或者您可以将if else 条件与响应设置为:

 if(responce!=null){
   HttpEntity entity=responce.getEntity();
  }
else{
     Toast.makeText(getApplicationContext(), "Connection to server failed.. Please try later..", Toast.LENGTH_LONG).show();
}

更不用说互联网许可了

于 2013-07-05T12:13:56.073 回答
1

您是否在清单中添加了这一行

<uses-permission android:name="android.permission.INTERNET" /> 

把它写在你的</application>标签外面

于 2013-07-05T11:59:07.607 回答
0

你在 Asyntask 响应中得到空字符串,你试图直接使用它。因此得到 NPE。

还要在清单中授予 INTERNET 权限以进行网络相关操作。

于 2013-07-05T11:56:30.157 回答