나는 Input xml로 android에서 Post 요청을 치고 xml로 결과를 얻고 싶다. 제발 안드로이드 자바에서 이것을 달성하는 방법을 말해주십시오. 나는 iPhone objective-c에서 이것을했다. 사전에어떻게 안드로이드에서 게시물 요청을 칠까?
감사
나는 Input xml로 android에서 Post 요청을 치고 xml로 결과를 얻고 싶다. 제발 안드로이드 자바에서 이것을 달성하는 방법을 말해주십시오. 나는 iPhone objective-c에서 이것을했다. 사전에어떻게 안드로이드에서 게시물 요청을 칠까?
감사
전화 연결 관리자 클래스 : 패스 URL 및 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
}
이것은 나를 위해 일하고 있습니다 ...... 만약 당신이 내가 당신을 안내 할 것임을 알리는 것보다 약간의 이슈에 직면한다면 –
질문을하기 전에 검색을 시도하십시오. 나는
당신이 여기에 요청하기 전에 어떤 코드 스스로를 시도 ...이 당신을 도움이 될 것 같아? – androidnoob
안녕하세요, 당신은 xml 형식의 입력을 작성했습니다, 당신은 xml이나 아닌 입력을 작성하기위한 코드가 필요하다는 뜻인가요? 곧 말해 –
XML을 입력으로 보내야하고 XML을 출력으로 가져옵니다. –