1

我正在使用以下用于连接到 Internet 进行解析的类,但是我在 HTTPResponse 上遇到了 NullPointerException。我还在清单文件中添加了 Internet 权限。这是我的代码

public class XMLParser {

    public String getXMLfromUrl(String Url){

        String xml =  null;

        try{

                 DefaultHttpClien httpClient = new DefaultHttpClient();
                 HttpPost httpPost = new HttpPost();
                 HttpResponse httpRes = httpClient.execute(httpPost);
                 final int status = httpRes.getStatusLine().getStatusCode();

                 if (status != HttpStatus.SC_OK) {
                     Log.d("meassage", "------------");
                 }

                 HttpEntity httpEnt = httpRes.getEntity();
                 xml = EntityUtils.toString(httpEnt);

        }catch(UnsupportedEncodingException e){
            e.printStackTrace();
        }catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return xml;
    }

    public Document getDomElement(String xml){

        Document doc = null;

        DocumentBuilderFactory  dbf = DocumentBuilderFactory.newInstance();

        try{

            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);

        }catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }


        return doc;
    }
}

帮助我找出我在哪里做错并建议需要更改。谢谢。这是一个堆栈跟踪

12-04 16:58:00.109: E/AndroidRuntime(2662): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gallery.parsing.madhuri/com.example.gallery.parsing.madhuri.MainActivity}: java.lang.NullPointerException
12-04 16:58:00.109: E/AndroidRuntime(2662):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at android.os.Looper.loop(Looper.java:137)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at android.app.ActivityThread.main(ActivityThread.java:4424)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at java.lang.reflect.Method.invoke(Method.java:511)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at dalvik.system.NativeStart.main(Native Method)
12-04 16:58:00.109: E/AndroidRuntime(2662): Caused by: java.lang.NullPointerException
12-04 16:58:00.109: E/AndroidRuntime(2662):     at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:496)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at com.example.gallery.parsing.madhuri.XMLParser.getXMLfromUrl(XMLParser.java:37)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at com.example.gallery.parsing.madhuri.MainActivity.onCreate(MainActivity.java:40)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at android.app.Activity.performCreate(Activity.java:4465)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-04 16:58:00.109: E/AndroidRuntime(2662):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-04 16:58:00.109: E/AndroidRuntime(2662):     ... 11 more
4

3 回答 3

2

您尚未为 httpPost 指定任何网址。这就是为什么响应为空。

它应该被初始化为

        HttpResponse httpResponse = new HttpResponse(url);
于 2012-12-04T11:48:34.900 回答
1

使用 AsyncTask 加载和解析数据,它将提高性能并且您不会收到 NetworkOnMainThreadException 因为 AsyncTask 是另一个线程。使用您的方法 (getXmlFromUrl(Url)) 在您的课程中发布以下代码。要执行,您只需调用(可能在 OnCreate() 上):

new YourTask().execute("");

这是代码:

    private class YourTask extends AsyncTask<String, Void, String> {
          String xmlContent = "";
           @Override
        protected String doInBackground(String... s) {

            //Here you have to make the loading / parsing tasks
            //Don't call any UI actions here. For example a Toast.show() this will couse Exceptions
            // UI stuff you have to make in onPostExecute method
              xmlContent=getXMLfromUrl(YOUR_URL);
        }

        @Override
        protected void onPreExecute() {
            // This method will called during doInBackground is in process
            // Here you can for example show a ProgressDialog
        }

        @Override
        protected void onPostExecute(Long result) {
            // onPostExecute is called when doInBackground finished
            // Here you can for example fill your Listview with the content loaded in doInBackground method


                  // here you can process he loading content in doInBackground
                  // for example:


  Toast.makeText(getApplicationContext(), xmlContent, Toast.LENGTH_SHORT).show();


        }

}

在这里您可以了解有关 AsyncTasks 的更多信息:

AsyncTask 开发者指南

于 2012-12-04T12:16:38.940 回答
0

o specified any url for httpPost

It should be initialized as

    HttpResponse httpResponse = new HttpResponse(url);

and Also create async class for them. call them in an asych class

于 2012-12-04T12:08:19.513 回答