2012-12-04 84 views
1

我正在使用以下类用于连接到因特网进行解析,但是我正在gwtting HTTPResponse上的NullPointerException。我还在清单文件中添加了Internet权限。这里是我的代码Http响应中的空指针异常

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 
+0

如果您也发布了堆栈跟踪,那将会很棒。 – span

+0

用堆栈跟踪更新了我的问题。 –

+0

你还没有为httpPost指定任何url。 – Sharmilee

回答

2

您还没有指定任何httpPost URL。 这就是为什么响应变为空。

应该初始化为

 HttpResponse httpResponse = new HttpResponse(url); 
+0

好吧,我做到了,但现在它的显示错误android.os.NetworkOnMainThreadException –

+0

这是bcoz你在主线程上做网络连接。 在工作线程中添加网络连接。 – Sharmilee

+0

你用什么android版本? XMLParser在自己的线程或主线程中运行? – Mikhaili

0

Ø指定httpPost

任何URL应该初始化为

HttpResponse httpResponse = new HttpResponse(url); 

并创建异步类他们。使用Asych类调用它们

1

使用AsyncTask加载和分析数据,它会提高性能,并且不会获得NetworkOnMainThreadException beacouse 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 developer Guide