我正在使用以下用于连接到 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