如何在android中使用REST使用Post方法获取XML信息?

如何在android中使用REST使用Post方法获取XML信息?,android,rest,xml-parsing,httpclient,Android,Rest,Xml Parsing,Httpclient,比如说, URI: http://99.99.99.99:8080/services/api/products/getProduct Method: POST Content-Type: application/xml 然后,我使用高级Rest客户端可以获取这些数据 <Products> <Product> <category>Apple</category> <productCode>A1</produ

比如说,

URI: http://99.99.99.99:8080/services/api/products/getProduct

Method: POST

Content-Type: application/xml
然后,我使用高级Rest客户端可以获取这些数据

<Products>
  <Product>
    <category>Apple</category>
    <productCode>A1</productCode>
  </Product>
  <Product>
    <category>Orange</category>
    <productCode>A2</productCode>
  </Product>
  <Product>
    <category>Banana</category>
    <productCode>A3</productCode>
  </Product>
....
</Products>

我认为,
System.out.println
的输出在logcat中不可见

您应该改为使用类:

Log.d("test", result);

您还必须确保您的请求是。

我收到了您的问题,主要是您在主线程中调用api

但您必须在后台线程中调用此api

使用AsynTask

    public class GetDetails extends AsyncTask<String, Void, String>{

    String response = null;
    @Override
    protected String doInBackground(String... strings) {
        try {

    HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    HttpPost httppost = null;

    httppost = new HttpPost("http://99.99.99.99:8080/services/api/products/getProduct");

    httppost.setHeader("Accept", "application/xml");
    httppost.setHeader("Content-type", "application/xml");

    HttpResponse httpResponse = httpClient.execute(httppost);

    InputStream inputStream = httpResponse.getEntity().getContent();
    if (inputStream != null)
         response = convertInputStreamToString(inputStream);
    else
        response = null;

    Log.v("data for list", response);

        } catch (Exception e) {
            Log.e("Cann", e.toString());
        }

        return strings[0];
    }

    @Override
    protected void onPostExecute(String result) {
    super.onPostExecute(result);
           if(response != null){
           XmlPullParserFactory factory =XmlPullParserFactory.newInstance();
             factory.setNamespaceAware(true);
             XmlPullParser xpp = factory.newPullParser();

             xpp.setInput(new StringReader (result));
             int eventType = xpp.getEventType();
             while (eventType != XmlPullParser.END_DOCUMENT) {
              if(eventType == XmlPullParser.START_DOCUMENT) {
                  System.out.println("Start document");
              } else if(eventType == XmlPullParser.END_DOCUMENT) {
                  System.out.println("End document");
              } else if(eventType == XmlPullParser.START_TAG) {
                  System.out.println("Start tag "+xpp.getName());
              } else if(eventType == XmlPullParser.END_TAG) {
                  System.out.println("End tag "+xpp.getName());
              } else if(eventType == XmlPullParser.TEXT) {
             System.out.println("End tag "+xpp.getName());       
              }
              eventType = xpp.next();
             }
         }
      }

private String convertInputStreamToString(InputStream inputStream)
        throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while ((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

}   

} 
public类GetDetails扩展异步任务{
字符串响应=null;
@凌驾
受保护的字符串背景(字符串…字符串){
试一试{
HttpParams httpParameters=新的BasicHttpParams();
int timeoutConnection=3000;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
//设置默认套接字超时(SO\U超时)
//以毫秒为单位,这是等待数据的超时。
int timeoutSocket=5000;
HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
DefaultHttpClient httpClient=新的DefaultHttpClient(httpParameters);
HttpPost HttpPost=null;
httppost=新的httppost(“http://99.99.99.99:8080/services/api/products/getProduct");
setHeader(“接受”、“应用程序/xml”);
setHeader(“内容类型”、“应用程序/xml”);
HttpResponse HttpResponse=httpClient.execute(httppost);
InputStream InputStream=httpResponse.getEntity().getContent();
如果(inputStream!=null)
响应=convertInputStreamToString(inputStream);
其他的
响应=空;
Log.v(“列表数据”,响应);
}捕获(例外e){
Log.e(“Cann”,e.toString());
}
返回字符串[0];
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
if(响应!=null){
XmlPullParserFactory工厂=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp=factory.newPullParser();
xpp.setInput(新的StringReader(结果));
int eventType=xpp.getEventType();
while(eventType!=XmlPullParser.END_文档){
if(eventType==XmlPullParser.START\u文档){
System.out.println(“开始文档”);
}else if(eventType==XmlPullParser.END_文档){
系统输出打印项次(“结束文件”);
}else if(eventType==XmlPullParser.START_标记){
System.out.println(“开始标记”+xpp.getName());
}else if(eventType==XmlPullParser.END_标记){
System.out.println(“结束标记”+xpp.getName());
}else if(eventType==XmlPullParser.TEXT){
System.out.println(“结束标记”+xpp.getName());
}
eventType=xpp.next();
}
}
}
私有字符串转换器InputStreamToString(InputStream InputStream)
抛出IOException{
BufferedReader BufferedReader=新的BufferedReader(
新的InputStreamReader(inputStream));
字符串行=”;
字符串结果=”;
而((line=bufferedReader.readLine())!=null)
结果+=行;
inputStream.close();
返回结果;
}   
} 

最后,调用
getXml()
方法更改为
new GetDetails().execute(“”)

网络操作必须在单独的线程中完成:在调用new GetDetails().execute(“”)之后,Cann日志显示它是java.io.FileNotFoundExceptionies,我可以从高级Rest客户端获取数据。可能是代码问题,Man,inputStream和reader之间存在问题,您有什么建议来解决这个问题吗?非常感谢。我在将输入流转换为字符串时遇到了一些问题,异常即将出现。我添加了函数can you Than,thx,you's hero,它最终可以在logcat中显示数据,现在我正在研究如何分离数据。苹果A1橙A2
    public class GetDetails extends AsyncTask<String, Void, String>{

    String response = null;
    @Override
    protected String doInBackground(String... strings) {
        try {

    HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    HttpPost httppost = null;

    httppost = new HttpPost("http://99.99.99.99:8080/services/api/products/getProduct");

    httppost.setHeader("Accept", "application/xml");
    httppost.setHeader("Content-type", "application/xml");

    HttpResponse httpResponse = httpClient.execute(httppost);

    InputStream inputStream = httpResponse.getEntity().getContent();
    if (inputStream != null)
         response = convertInputStreamToString(inputStream);
    else
        response = null;

    Log.v("data for list", response);

        } catch (Exception e) {
            Log.e("Cann", e.toString());
        }

        return strings[0];
    }

    @Override
    protected void onPostExecute(String result) {
    super.onPostExecute(result);
           if(response != null){
           XmlPullParserFactory factory =XmlPullParserFactory.newInstance();
             factory.setNamespaceAware(true);
             XmlPullParser xpp = factory.newPullParser();

             xpp.setInput(new StringReader (result));
             int eventType = xpp.getEventType();
             while (eventType != XmlPullParser.END_DOCUMENT) {
              if(eventType == XmlPullParser.START_DOCUMENT) {
                  System.out.println("Start document");
              } else if(eventType == XmlPullParser.END_DOCUMENT) {
                  System.out.println("End document");
              } else if(eventType == XmlPullParser.START_TAG) {
                  System.out.println("Start tag "+xpp.getName());
              } else if(eventType == XmlPullParser.END_TAG) {
                  System.out.println("End tag "+xpp.getName());
              } else if(eventType == XmlPullParser.TEXT) {
             System.out.println("End tag "+xpp.getName());       
              }
              eventType = xpp.next();
             }
         }
      }

private String convertInputStreamToString(InputStream inputStream)
        throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while ((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

}   

}