如何从Android的值传递给PHP的Web服务和检索吗? [英] How to pass a value from android to php web service and retrieve it?
问题描述
我试图将值传递给我的PHP web服务。我已经使用这个code为通过名值:
I'm trying to pass a value to my php webservice. I already use this code for pass the "name" value :
private class MyAsyncTask extends AsyncTask<String, Void, Void> {
protected Void doInBackground (String... params)
{
Intent intent = getIntent();
String name = intent.getStringExtra("KEY_NAME");
//HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/secure_login/get_data_user.php");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
nameValuePair.add(new BasicNameValuePair("KEY_NAME", name));
DefaultHttpClient hc = new DefaultHttpClient();
// HttpResponse response = hc.execute(httppost);
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e){
// writing error to log
e.printStackTrace();
}
try {
HttpResponse response = hc.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream inStream = entity.getContent();
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
和这一点,流转换为字符串。
And this, for convert Stream to String.
protected String convertStreamToString(InputStream inStream)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
inStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return sb.toString();
}
}
但我只拿到了日志猫这样的响应:
org.apache.http.message.BasicHtt$p$psponse@43e4c068
But I only got this response in log cat : org.apache.http.message.BasicHttpResponse@43e4c068
我需要通过名称值,使我的PHP Web服务可以获取并做查询是这样的:
I need to pass the "name" value so my php webservice can retrieve and do the query like this :
if (isset($_GET['name'])) {
$name = $_GET['name'];
require_once 'DB_Functions.php';
$db = new DB_Functions();
$result = mysql_query("SELECT name, email from users where name = '$name'");
我如何应该修复它?谢谢你在前进。
How do I should fix it? Thank you in advance.
推荐答案
您在Android的发送POST请求,并试图检索与 $ PHP中的参数_ GET
。
You are sending a post request in android and trying to retrieve the parameter in PHP with $_GET
.
尝试通过 $ _ POST访问名称变量
:
if (isset($_POST['name']))
$name = mysql_real_escape_string($_POST['name']);
require_once 'DB_Functions.php';
$db = new DB_Functions();
$result = mysql_query("SELECT name, email from users where name = '$name'");
重要:不要忘记你把他们之前在SQL语句中使用 mysql_real_escape_string
来避免SQL注入逃避字符串
Important: Don't forget to escape the strings before you put them in your SQL statement with mysql_real_escape_string
to avoid SQL injections.
这篇关于如何从Android的值传递给PHP的Web服务和检索吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!