【问题标题】:How to send Parameters using HTTP Post in Android Studio如何在 Android Studio 中使用 HTTP Post 发送参数
【发布时间】:2016-03-16 13:08:38
【问题描述】:

我想使用 HTTP Post 方法发送两个更多的参数。

这是我的 ServiceHandler.java

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class ServiceHandler {

    static String response = null;
    public final static int GET = 1;
    public final static int POST = 2;

    public ServiceHandler() {

    }

    /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * */
    public String makeServiceCall(String url, int method) {
        return this.makeServiceCall(url, method, null);
    }

    /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * @params - http request params
     * */
    public String makeServiceCall(String url, int method,
                                  List<NameValuePair> params) {
        try {
            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }

                httpResponse = httpClient.execute(httpPost);

            } else if (method == GET) {
                // appending params to url
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);

                httpResponse = httpClient.execute(httpGet);

            }
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;

    }
}

这是我的活动中的代码

private static String url = "http://example.com/test.php;

@Override
        protected Void doInBackground(Void... arg0) {

            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.POST);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    status = jsonObj.getJSONArray(status_NODE);
                    JSONObject value = status.getJSONObject(0);

                    // Saving Slots Availability in String status
                    // availability = value.getString(availability_NODE);
                    availability = "notFree";

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

我现在如何发送参数。例如我需要发送一个值为 xyz 的参数 abc

谢谢

【问题讨论】:

    标签: android json xml


    【解决方案1】:

    试试看

    HttpPost httpPost = new HttpPost(url);
    // adding post params 
    List<NameValuePair> params = new ArrayList<NameValuePair>(2);
    params.add(new BasicNameValuePair("abc", "xyz"));
    if (params != null) {
        httpPost.setEntity(new UrlEncodedFormEntity(params));
    }
    httpResponse = httpClient.execute(httpPost
    

    【讨论】:

    • 我想从我的Activity而不是从ServiceHandler.java发送参数可以吗?
    • 那就试试吧List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); params.add(new BasicNameValuePair("abc", "xyz"));sh.makeServiceCall(url, ServiceHandler.POST, params);
    • 您能否将您的代码发送至 thuongle.it@gmail.com。我会在这里检查更多!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2013-11-18
    • 2013-01-11
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多