如何使用REST using Post方法在android中获取XML信息?
How to use REST using Post Method to get XML information in android?
例如,
URI: http://99.99.99.99:8080/services/api/products/getProduct
Method: POST
Content-Type: application/xml
然后,我用Advanced Rest Client可以得到这些数据
<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>
如果我想存储所有类别和产品代码怎么办。
我应该使用什么样的方法?
HttpPost? XML 解析器?
这是我尝试的示例代码,但我从 logcat.
中一无所获
public void getXml(){
String result = "";
HttpClient client = new DefaultHttpClient();
try {
HttpPost post = new HttpPost("http://99.99.99.99:8080/services/api/products/getProduct");
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
client.getConnectionManager().shutdown();
}
System.out.println(result);
}
我认为 System.out.println
的输出在 logcat 中不可见。
您应该使用 Log class 代替:
Log.d("test", result);
此外,您必须确保您的请求是 done in a separate thread。
我遇到了你的问题,主要是你在主线程中调用了 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;
}
}
最后调用 getXml()
方法的地方更改为 new GetDetails().execute("")
例如,
URI: http://99.99.99.99:8080/services/api/products/getProduct
Method: POST
Content-Type: application/xml
然后,我用Advanced Rest Client可以得到这些数据
<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>
如果我想存储所有类别和产品代码怎么办。
我应该使用什么样的方法?
HttpPost? XML 解析器?
这是我尝试的示例代码,但我从 logcat.
中一无所获public void getXml(){
String result = "";
HttpClient client = new DefaultHttpClient();
try {
HttpPost post = new HttpPost("http://99.99.99.99:8080/services/api/products/getProduct");
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
client.getConnectionManager().shutdown();
}
System.out.println(result);
}
我认为 System.out.println
的输出在 logcat 中不可见。
您应该使用 Log class 代替:
Log.d("test", result);
此外,您必须确保您的请求是 done in a separate thread。
我遇到了你的问题,主要是你在主线程中调用了 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;
}
}
最后调用 getXml()
方法的地方更改为 new GetDetails().execute("")