繁体   English   中英

如何在Android中使用参数发送“ POST”请求?

[英]How to send “POST” request with params in Android?

我正在从网站爬网数据。 为此,我需要发布到此API URL: https : //api.import.io/store/connector/3b14652e-4785-4402-b1a8-d9363c8e988e/_query?_apikey=

为了获得响应,我还需要发送一些原始数据。 该数据基本上是从另一个POST请求检索到的响应Json数据。 然后,Api将对原始数据执行查询并发送响应。

我该如何针对Android App执行此操作?

这是我的POST HTML代码

POST /store/connector/3b14652e-4402-b1a8-d9363c8e988e/_login?_apikey=mykey HTTP/1.1 
Host: api.import.io
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 7f95c060-0c59-d92b-xxxx-9b9184527208
{
"username": "user",
"password": "pass"
}

这样的事情应该可以帮助您解决问题或使您走上正确的道路:

URL url = new URL("your_api_url");

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);

Uri.Builder builder = new Uri.Builder();
builder.appendQueryParameter("data"  , "json_value");
String query = builder.build().getEncodedQuery();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
writer.write(query);
writer.flush();
writer.close();

int response = conn.getResponseCode();
if(response == HttpURLConnection.HTTP_OK)
{
    InputStream bis = new BufferedInputStream(conn.getInputStream());
    //bis is your json do whatever you want with it
}
conn.disconnect();

使用Volley帖子库实现这一目标

将此行添加到gradle中

compile 'com.mcxiaoke.volley:library:1.0.16'

写下此助手Volley Request类

public class JSONObjectAuthRequest extends JsonObjectRequest
{
    private final Map<String, String> headers;

    public JSONObjectAuthRequest(int method, Map<String, String> headers, String url, JSONObject json, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener)
    {
        super(method, url, json, listener, errorListener);

        this.headers = headers;
   }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError
    {
        return headers;
    }
 }

并使用上述课程传递您的要求

JSONObjectAuthRequest request = new JSONObjectAuthRequest(Request.Method.POST, null, url, postParam, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                //  write your logic here
                Log.d(TAG, "Success");
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Log.d(TAG, "Failure");
            }
        });

        RequestManager.getInstance(getContext()).addToRequestQueue(request);

这应该有所帮助。

干杯,

你可以做这样的事情:

            try {
                        URL reqURL = new URL(link); //the URL we will send the request to
                        HttpURLConnection request = (HttpURLConnection) (reqURL.openConnection());
                        request.setDoOutput(true);
                        request.addRequestProperty("Content-Length", Integer.toString(parameters.length()));
//where parameter is a string written like this : paramKey1=value1&paramKey2=value2
                        request.addRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //add the content type of the request, most post data is of this type
                        request.setRequestMethod("POST");
                        request.connect();
                        OutputStreamWriter writer = new OutputStreamWriter(request.getOutputStream());
                        writer.write(parameters);
                        writer.flush();
                        writer.close();
                        int responseCode = request.getResponseCode();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }`

最重要的是,不要忘记将这些行执行到新线程中,因为您不能在ui线程中执行此操作。 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM