如何在android中发布帖子请求?

时间:2012-03-14 06:05:09

标签: android xml post xmlhttprequest request

我想在android中使用Input xml命中一个Post请求,并输出为xml。请告诉我在android java中实现这个的方法。我在iPhone objective-c中完成了这个。

提前致谢

2 个答案:

答案 0 :(得分:3)

致电Connection Manager课程: 使用此代码发送请求:传递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
}

答案 1 :(得分:1)

在提出问题之前尝试进行一些搜索。我想这会对你有帮助......

Android, sending XML via HTTP POST (SOAP)

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