php - 使用 HttpURLConnection 时,mysql 上的 $_POST 为空

标签 php android mysql post httpurlconnection

我正在使用 HttpURLConnection 从 mysql 数据库表检索图像的路径。但我只需要给定用户名的路径,因此我使用 POST 将用户名拖到服务器。

public class ImageDownload {
public String sendPostRequest(String requestURL,
                              HashMap<String, String> postDataParams) {

    URL url;

    StringBuilder sb = new StringBuilder();
    try {
        url = new URL(requestURL);

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


        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            sb = new StringBuilder();
            String response;
            while ((response = br.readLine()) != null){
                sb.append(response);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for (Map.Entry<String, String> entry : params.entrySet()) {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}}

这里是我的 doInBackground,我设置要传递的用户名并调用 ImageDownload 类

             ImageDownload rh = new ImageDownload();

 @Override
        protected String doInBackground(String... strings) {
            UserLocalStore userLocalStore = new UserLocalStore(GridViewActivity.this);
            String username = userLocalStore.getLoggedInUser().username;
            HashMap<String,String> data = new HashMap<>();

            data.put(KEY_USERNAME, username);
            String result = rh.sendPostRequest(GET_IMAGE_URL,data);

            return result;
        }

服务器上的php是

 <?php
require_once('dbConnect.php');

$username = $_POST["username"];

$sql = "select image from photos WHERE username =?";

$res = mysqli_prepare($con,$sql);
 mysqli_stmt_bind_param($res, "s", $username);

$result = array();

while($row = mysqli_fetch_array($res)){
    array_push($result,array('url'=>$row['image']));
}

echo json_encode(array("result"=>$result));

mysqli_close($con);
 ?>

我有一个问题。

返回 sb.toString()

来自 ImageDownload 类是空的!我检查了服务器,$_POST 似乎有问题,因为它显然是空的。事实上,如果我删除条件“WHERE username =?”在服务器 php 文件上,我成功地从数据库表中检索了整个路径列表。但这不是我需要的。 $_POST 变量有什么问题以及为什么无法正确上传? 非常感谢

最佳答案

根据这个answer ,您需要将这些属性设置为 POST 请求才能使其正常工作:

conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded");
conn.setRequestProperty( "Accept", "*/*" );

关于php - 使用 HttpURLConnection 时,mysql 上的 $_POST 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33920024/

相关文章:

android - 如何将文件保存到内部存储中的子目录

php - 如何检查请求是否是 Symfony 2 或 Symfony 3 中的 POST 或 GET 请求

php - 内联 PHP/HTML 三元 If

php - 从多列中获取结果 MySQL PHP

android - Kotlin - 如何创建类似于 TextView.setText(String) 的类成员函数,可以调用为 TextView.text = ""

mysql - 计算MySQL中其他平均值的平均值

javascript - Laravel 和 Firebase 实时数据库连接

android - 删除后在 Google Play 上更新 Android 应用程序

Mysql更新子查询指定目标表

mysql - 本地 WordPress 安装 - wp-admin 错误