gpt4 book ai didi

java - 发出 HTTPS 请求时出现问题

转载 作者:行者123 更新时间:2023-12-01 13:17:46 28 4
gpt4 key购买 nike

我有这个异步任务:

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我已在发布的代码中更改了网站名称和数据库详细信息。

最佳答案

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

关于java - 发出 HTTPS 请求时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22329548/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com