0

I want to hit a Post request in android with an Input xml and get output as xml. Please tell me the way to achieve this in android java. I have done this in iPhone objective-c.

Thanks in advance

Sanchit Paurush
  • 6,114
  • 17
  • 68
  • 107

2 Answers2

3

Call Connection Manager Class: Send Request Using this Code: pass url and 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();
        }

Connection Manager Class:

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();
    }
}

Request Type Class:

public enum RequestMethod
{
GET,
POST,
PUT
}
aalionline
  • 448
  • 2
  • 6
  • 21
1

Try to do some search before asking a question. I think this will help you...

Android, sending XML via HTTP POST (SOAP)

https://stackoverflow.com/a/7739616/1029059

Community
  • 1
  • 1
Scorpion
  • 6,831
  • 16
  • 75
  • 123