【问题标题】:How to send http post request from Android?如何从 Android 发送 http post 请求?
【发布时间】:2016-11-19 08:57:05
【问题描述】:

我是 android 新手,现在我已经厌倦了寻找关于 android HTTP 请求的最佳教程。

我正在尝试将这些参数发送到 phpServer,

  • Name = "某个名字"

  • email = "一些电子邮件"

我们可以在 MainActivity.java 文件中执行此操作吗?这需要任何jar(库)文件吗? 欢迎提出任何建议。

我试过了,

@Override
public void onClick(View view) {
    if(view == btnSubscribe){
        HttpURLConnection client = null;
        try {
            URL url = new URL("http://www/somewebsite.com/API/SUBSCRIBE");
            client = (HttpURLConnection) url.openConnection();
            client.setRequestMethod("POST");
            client.setRequestProperty("name","test one");
            client.setRequestProperty("email","text@gmail.com");
            client.setDoOutput(true);

            OutputStream outputPost = new BufferedOutputStream(client.getOutputStream());
            outputPost.flush();
            outputPost.close();
            client.setChunkedStreamingMode(0);

        } catch(MalformedURLException error) {
            showError("Handles an incorrectly entered UR");
        }
        catch(SocketTimeoutException error) {
            showError("Handles URL access timeout");
        }
        catch (IOException error) {
            showError("Handles input and output errors");
        }finally {
            if(client != null)
            client.disconnect();
        }
    }
}

【问题讨论】:

  • @Sahan 通过this
  • @IntelliJAmiya 抱歉,我清除了所有代码,因为对我不起作用。现在我在一个新项目中。
  • @Nisarg 谢谢。
  • @IntelliJAmiya 伙计们现在可以看看吗?我试过了。
  • name 应该是Name

标签: php android http-post


【解决方案1】:

可以确认 Jitendra ramoliya 的答案是正确的,尽管它没有指定“数据”是什么。

数据有一个字符串,其中第一个编码值是 POST 索引,第二个是该索引的值,用“=”分隔。

这一点一开始并不明显,而且 android 文档没有涵盖数据连接。

$_POST['Email'] 等效于URLEncoder.encode("Email", "UTF-8") 使用此方法 |并添加它的价值是

URLEncoder.encode("Email", "UTF-8") + "="
                    + URLEncoder.encode(email, "UTF-8")

【讨论】:

    【解决方案2】:

    我正在使用此代码并且也可以正常工作。试试这个代码。

    public static String httpPostRequest(Context context, String url, String email) {
            String response = "";
            BufferedReader reader = null;
            HttpURLConnection conn = null;
            try {
                LogUtils.d("RequestManager", url + " ");
                LogUtils.e("data::", " " + data);
                URL urlObj = new URL(url);
    
                conn = (HttpURLConnection) urlObj.openConnection();
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    
                data += "&" + URLEncoder.encode("Email", "UTF-8") + "="
                        + URLEncoder.encode(email, "UTF-8");
    
    
                wr.write(data);
                wr.flush();
    
                LogUtils.d("post response code", conn.getResponseCode() + " ");
    
                int responseCode = conn.getResponseCode();
    
    
    
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line = null;
    
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
    
                response = sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
                LogUtils.d("Error", "error");
            } finally {
                try {
                    reader.close();
                    if (conn != null) {
                        conn.disconnect();
                    }
                } catch (Exception ex) {
                }
            }
            LogUtils.d("RESPONSE POST", response);
            return response;
        }
    

    【讨论】:

      【解决方案3】:

      首先,大多数 httpClint 现在已被弃用,所以如果您现在可以使用 httpUrlConnection 来做同样的事情。

      你可以参考 here

      如果你想使用库,那么你可以使用

      这两个是最好的库。

      干杯

      【讨论】:

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