【问题标题】:Sending a parameterized HTTP POST request from Android从 Android 发送参数化 HTTP POST 请求
【发布时间】:2016-06-02 20:34:46
【问题描述】:

问题:

我在从 Android 设备访问云数据时遇到问题。我正在使用 POST 方法并期待 JSON 响应。我相信我收到 403(禁止)错误。但我可以使用 curl 访问相同的数据。所以我相信我做错了什么,因此寻求某人的帮助。

背景

这是我试图复制的 curl 命令字符串。我收到对此命令的有效响应。

curl -X POST --data "token={String}&param1={int}&param2={int}" https://www.example.com/api/dir1/dir2"

下面是安卓代码。

        String post_url = "https://www.example.com/api/dir1/dir2";
        Thread thread = new Thread(new Runnable() {
        public void run() {
            try {

                String urlParameters = "token={Key-string}&param1={int}&param2={int}";
                byte[] postData = urlParameters.getBytes();
                int postDataLength = postData.length;

                URL url = new URL(post_url);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(10000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Accept", "application/json");
                conn.setRequestProperty("charset", "utf-8");
                conn.setRequestProperty("Content-Length",Integer.toString(postDataLength));
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.connect();

                OutputStream os = conn.getOutputStream();
                os.write(urlParameters.toString().getBytes("UTF-8"));
                os.close();

                InputStream stream = conn.getInputStream();

                BufferedReader in = new BufferedReader( new InputStreamReader(stream ));
                String data = null;
                StringBuffer response = new StringBuffer();

                while ((data = in.readLine()) != null) {
                    response.append(data);}
                in.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    thread.start();

参考资料:

【问题讨论】:

  • urlParameters的类型是什么?
  • 在继续之前确定它的含义。查一下。
  • 我不太确定。我查看了一些示例 stackoverflow 并尝试了不同的东西。但是,如果您查看 curl 命令,我没有指定任何类型。我认为这是一种默认类型。
  • 你应该先确定 403 的意思是我说的。查一下。
  • 它的硬编码。使用 Okhttp 或 volley 让我们的生活更轻松

标签: android json http-post


【解决方案1】:

您可以尝试在 Postman(谷歌浏览器插件)中拨打电话,然后点击 代码 链接。 Postman 会以多种语言向您生成请求,如 PHP、java、C 等。

例如:

添加到你的 gradle:

compile 'com.squareup.okhttp3:okhttp:3.5.0'

在某处打电话:

    new Thread() {
       public void run() {
           makeCall();
       }
    }.start();

创建该类:

    public void makeCall() {

    OkHttpClient client = new OkHttpClient();

    MediaType mediaType = MediaType.parse("application/json");
    RequestBody body = RequestBody.create(mediaType, "{\n   \"param1\":\"data1\"}");
    Request request = new Request.Builder()
            .url("https://your_url_here")
            .post(body)
            .addHeader("content-type", "application/json")
            .addHeader("cache-control", "no-cache")
            .build();

    try {
        Response response = client.newCall(request).execute();

        Log.e("okmsg1", response.toString());
        Log.e("okmsg2", response.message());
        Log.e("okmsg3", response.body().string());

    } catch (IOException e) {
        Log.e("err", e.getMessage());
    }

}

【讨论】:

    猜你喜欢
    • 2012-12-04
    • 2016-11-19
    • 2017-05-03
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多