我正在尝试将一个字符串(例如" name")传递给我的PHP脚本,该脚本需要获取" name"来自Android应用程序的$ _POST,然后在SELECT查询中使用它并将其传回Android应用程序。
我已经完成了所有工作,除了我只能将字符串传递给PHP脚本,但我无法在SELECT查询中使用它。这是我的代码:
PHP代码:
<?php
include "../dbc.php";
//include "get_id.php";
$value = $_POST["value"];
$response = array();
$q=$dbh->prepare("SELECT * FROM users WHERE user_name = :value");
$q->bindParam(':value', $value);
$q->execute();
while ($row = $q->fetch(PDO::FETCH_ASSOC))
{
$first_name=$row['first_name'];
$last_name=$row['last_name'];
$company_name=$row['company_name'];
$loc=$row['loc'];
$tel=$row['tel'];
$website=$row['website'];
$aboutme=$row['aboutme'];
array_push($response, array("first_name"=>$first_name,"last_name"=>$last_name,"company_name"=>$company_name,"loc"=>$loc,"tel"=>$tel,"website"=>$website,"aboutme"=>$aboutme));
}
echo json_encode(array("server_response"=>$response));
?>
Android代码:
class GetUserSettings extends AsyncTask<Void,Void,String> {
String json_get_settings_url;
@Override
protected void onPreExecute() {
json_get_settings_url = "http://url.com/json.php";
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected String doInBackground(Void... Voids) {
URL url = null;
try {
url = new URL(json_get_settings_url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader (new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while((JSON_STRING = bufferedReader.readLine()) != null) {
stringBuilder.append(JSON_STRING+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String JSON_STRING) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
String value = settings.getString("user_name", "");
//this is where it starts parsing the JSON from the URL
try {
JSONObject jsonResponse = new JSONObject(JSON_STRING);
JSONArray jsonMainNode = jsonResponse.optJSONArray("server_response");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
user_name = jsonChildNode.optString(value);
et_user_name.setText(value);
first_name = jsonChildNode.optString("first_name");
et_first_name.setText(first_name);
last_name = jsonChildNode.optString("last_name");
et_last_name.setText(last_name);
company_name = jsonChildNode.optString("company_name");
et_company_name.setText(company_name);
loc = jsonChildNode.optString("loc");
et_loc.setText(loc);
tel = jsonChildNode.optString("tel");
et_tel.setText(tel);
website = jsonChildNode.optString("website");
et_website.setText(website);
aboutme = jsonChildNode.optString("aboutme");
et_aboutme.setText(aboutme);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Something is wrong. Please restart the app or contact us.",Toast.LENGTH_LONG).show();
}
}
}
所以从代码中可以看出,我正在尝试传递String&#34;值&#34;到我的PHP代码,然后分配&#34;值&#34; $ value然后在SELECT查询中使用$ value但我怎么能传递String&#34;值&#34;然后执行查询,以便我可以使用JSON来解析数据?
答案 0 :(得分:0)
您可以使用ContentValues()将字符串传递给php amd返回select查询。
ContentValues values = new Content Values(); values.put(&#34;串&#34;,变量);
你可以传递网址
http://example.com/dir/page.php?var=variable
在php端
$ var = $ _GET [&#39;变量&#39;]: 现在,您可以在select查询中传递此$ var。
希望这有帮助。