Я хочу получить запрос на отправку в android с помощью ввода xml и получить вывод в виде xml. Скажите, пожалуйста, как это сделать в Android-сообществе. Я сделал это в iPhone цели-c.Как ударить по почте в андроид?
Заранее спасибо
Я хочу получить запрос на отправку в android с помощью ввода xml и получить вывод в виде xml. Скажите, пожалуйста, как это сделать в Android-сообществе. Я сделал это в iPhone цели-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 в качестве вывода –