我是使用XML解析的新手。我编写了一个XMLParser.java类来解析xml。但是当我尝试它时,它不能与android studio中的DefaultHTTPClient
一起使用。任何人都可以帮我解决如何使用Http URlConnection
下面的代码?
public String getXmlFromUrl(String url)
{
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
答案 0 :(得分:0)
试一试:
public String getXmlFromUrl(String url) {
String xml=null;
// defaultHttpClient
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost();
URI uri = new URI(url);
httpPost.setURI(uri);
HttpResponse httpResponse = httpClient.execute(httpPost);
.........
}
如果不起作用,请发布您的logcat