【问题标题】:Trouble when making a HTTPS request发出 HTTPS 请求时出现问题
【发布时间】:2014-04-15 06:58:23
【问题描述】:

我有这个异步任务:

public class likeTheJoke extends AsyncTask<Void, Integer, Void>{        

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

        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://site.com/android/jokes/updateLikes.php");

        try {
             // Add data
            String encodedUrl = URLEncoder.encode("Joke 7", "UTF-8"); // recently added after a comment suggestion
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("jokeName", encodedUrl));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }       
}

我正在尝试将参数传递给我的 php 脚本并更新数据库中的值。 使用上面的代码,我的链接应该看起来像:https://site.com/android/jokes/updateLikes.php?jokeName=Joke 7

问题是在请求之后,什么都没有发生。我知道这很正常,因为HTTPS,但我真的找不到使请求工作的方法,即使这里有大约 10 个答案。你能指导我并告诉我我错过了什么吗?我知道它非常小,但我无法发现它。

这是我的 php 代码(只是一个附加信息):

<?php
$jokeName = urldecode($_GET["jokeName"]); // urldecode added after a comment suggestion.

$con=mysqli_connect("localhost","user","pass","database");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"UPDATE JOKES SET likes = likes+1
WHERE jokeName='$jokeName'");

mysqli_close($con);
?>

P.S我已在发布的代码中更改了站点名称和数据库详细信息。

【问题讨论】:

  • 你知道在 Joke 和 7 之间有一个空格吗?
  • @blackbelt 代码已编辑。同样的问题正在发生。 + UrlEncodedFormEntity 应该可以胜任。
  • 你怎么确定什么都没发生?
  • @JoeriHendrickx 数据库中的值没有得到更新。

标签: java php android https android-asynctask


【解决方案1】:

如果有人遇到同样的问题,我是这样解决的:

    try{
                String encodedUrl = URLEncoder.encode("name with spaces", "UTF-8");
                URL url = new URL("https://site.com/android/dir/file.php?paramName="+encodedUrl);
                URLConnection urlConnection = url.openConnection();
                @SuppressWarnings("unused")
                InputStream in = urlConnection.getInputStream();
                }catch(Exception e){
                    e.printStackTrace();
                }
    }

在 php 中,您需要从 url 中转义和解码参数,例如:

$categoryParam= mysql_real_escape_string(urldecode($_GET["Param"]));

这可能不是最好的方法,但它确实有效。

【讨论】:

    【解决方案2】:

    您正在发送以jokeName=joke 7 作为帖子正文(实体)的帖子。但在 php 方面,您试图将其作为 GET 参数(url 参数)。所以笑话 id 不会传递到 PHP 端。

    在 php 端进行一些简单的日志记录应该可以揭示这一点。

    要么添加jokeName=Joke 7 作为url 参数,要么在php 端使用$_POST['jokeName']

    【讨论】:

      【解决方案3】:

      您应该使用“url 编码”来确保目标字符串中的空白被正确转换。

      【讨论】:

      • 我刚刚编辑了java和php代码。同样的问题正在发生。 UrlEncodedFormEntity 也应该完成这项工作。
      猜你喜欢
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 2019-08-08
      • 2011-01-21
      • 2021-01-19
      相关资源
      最近更新 更多