我想用一個Input xml在android中打一個Post請求並獲取輸出爲xml。請告訴我在android java中實現這一點的方式。我已經在iPhone objective-c中做了這個。如何在android中打一個帖子請求?
在此先感謝
我想用一個Input xml在android中打一個Post請求並獲取輸出爲xml。請告訴我在android java中實現這一點的方式。我已經在iPhone objective-c中做了這個。如何在android中打一個帖子請求?
在此先感謝
呼叫連接管理器類: 發送請求使用此代碼:通網址和xml-REQ
String url=" Enter URL Here"
ConnectionManager connectionManger = new ConnectionManager(url);
connectionManger.AddParam("xml_req", xml_req);
try {
response = connectionManger.sendRequest(RequestMethod.POST);
} catch (Exception e) {
e.printStackTrace();
}
連接管理器類:
import android.content.Context;
import com.mutmonix.series.bo.RequestMethod;
public class ConnectionManager {
private ArrayList <NameValuePair> params;
private ArrayList <NameValuePair> headers;
private String url;
public static Context context;
File tempDir;
public ConnectionManager(String url) {
this.url = url;
params = new ArrayList<NameValuePair>();
headers = new ArrayList<NameValuePair>();
}
public String sendRequest(RequestMethod method)throws Exception {
return callServer(method);
}
public void AddParam(String name, String value)
{
params.add(new BasicNameValuePair(name, value));
}
public void AddHeader(String name, String value)
{
headers.add(new BasicNameValuePair(name, value));
}
public String callServer(RequestMethod method) throws Exception {
String xmlResponse = "";
switch(method) {
case GET:
{
//add parameters
String combinedParams = "";
if(!params.isEmpty()){
combinedParams += "?";
for(NameValuePair p : params)
{
String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(),"UTF-8");
if(combinedParams.length() > 1)
{
combinedParams += "&" + paramString;
}
else
{
combinedParams += paramString;
}
}
}
HttpGet request = new HttpGet(url + combinedParams);
//add headers
for(NameValuePair h : headers)
{
request.addHeader(h.getName(), h.getValue());
}
xmlResponse = executeRequest(request, url);
break;
}
case POST:
{
HttpPost request = new HttpPost(url);
//add headers
for(NameValuePair h : headers)
{
StringEntity entity = new StringEntity(h.getValue(), "UTF-8");
request.setEntity(entity);
request.addHeader("Accept", "application/xml");
request.addHeader("Content-Type", "application/xml");
}
if(!params.isEmpty()){
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
xmlResponse = executeRequest(request, url);
break;
}
case PUT:
{
HttpPut request = new HttpPut(url);
//add headers
for(NameValuePair h : headers)
{
StringEntity entity = new StringEntity(h.getValue(), "UTF-8");
request.setEntity(entity);
request.addHeader("Accept", "application/xml");
request.addHeader("Content-Type", "application/xml");
}
if(!params.isEmpty()){
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
xmlResponse = executeRequest(request, url);
break;
}
}
return xmlResponse;
}
private String executeRequest(HttpUriRequest request, String url) throws Exception
{
//HttpClient client = new DefaultHttpClient();
DefaultHttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
// Set Connection TimeOut
HttpConnectionParams.setConnectionTimeout(params, 30000);
HttpResponse httpResponse;
String xmlResponse = "";
httpResponse = client.execute(request);
int responseCode = httpResponse.getStatusLine().getStatusCode();
String message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
xmlResponse = convertStreamToString(instream);
// Closing the input stream will trigger connection release
instream.close();
}
return xmlResponse;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
請求類型分類:
public enum RequestMethod
{
GET,
POST,
PUT
}
這是爲我工作......如果你遇到一些問題,不要讓我知道我會相應地引導你 – 2012-03-14 06:30:15
嘗試在提問之前做一些搜索。我認爲這將有助於你...
您是否嘗試過自己的任何代碼,詢問這裏之前? – androidnoob 2012-03-14 06:07:09
嗨,你有沒有寫入XML格式的輸入,我的意思是你需要代碼寫入輸入XML或不?告訴我很快 – 2012-03-14 06:08:45
我需要發送一個XML作爲輸入並將XML作爲輸出 – 2012-03-14 06:10:46