如何在Android Studio 1.4.1中进行XML解析?

时间:2015-11-13 03:52:50

标签: android

我是使用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;
}

1 个答案:

答案 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