I have this async task:
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;
}
}
I'm trying to pass a parameter to my php script and to update a value in the database. With the code above my link should be looking like: https://site.com/android/jokes/updateLikes.php?jokeName=Joke 7
The problem is that after the request, nothing is happening. I know that it's normal, because of the HTTPS
, but I really can't find a way to make the request working, even when there are about 10 answers here. Can you guid me and tell me what I'm missing? I know that it is something really small, but I'm not able to spot it.
Here is my php code(just an additional info):
<?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);
?>
PS I have changed the site name and DB details in teh posted code.
您应该使用“ URL编码”来确保目标字符串中的空白已正确转换。
You are sending a post with the jokeName=joke 7
as the post body (entity). But on the php side, you're trying to get it as a GET param (url parameter). So the joke id is not passed to the PHP side.
Some simple logging on php side should reveal this.
Either add the jokeName=Joke 7
as a url parameter, or use $_POST['jokeName']
on the php side.
If someone is in trouble with the same issue, here is how i fixed it:
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();
}
}
in php you need to escape and decode the parameter from the url like:
$categoryParam= mysql_real_escape_string(urldecode($_GET["Param"]));
It might not be the best way to do it, but it is working.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.