我正在使用POST从android到php文件传递url中的一些值我得到空值。
请帮忙!
我的其他PHP代码运行良好,但唯一的问题是它无法从网址中提取值。
我的PHP代码:
$b = $_POST['bal'];
$balance_tobe_added = intval($b);
$h1 = $_POST['hotcode'];
$hotcode = intval($h1);
$date = $_POST['date'];
$time = $_POST['time'];
$city = $_POST['city'];
echo $b.$h.$date.$time.$city;
我的android代码是:
private void updateDB(String balance, String hotcode, String date, String time, String city) {
class DBupdate extends AsyncTask<String, Void, String> {
@Override
protected void onCancelled() {
super.onCancelled();
Toast.makeText(MainActivity.this,"error",Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
}
@Override
protected String doInBackground(String... strings) {
String res = "failed";
try {
URL url = new URL("http://www.mrhot.in/Customer/test/test.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
StringBuilder input = new StringBuilder();
input.append(URLEncoder.encode("bal", "UTF-8"));
input.append(URLEncoder.encode("=", "UTF-8"));
input.append(URLEncoder.encode(strings[0], "UTF-8"));
input.append(URLEncoder.encode("&", "UTF-8"));
input.append(URLEncoder.encode("hotcode", "UTF-8"));
input.append(URLEncoder.encode("=", "UTF-8"));
input.append(URLEncoder.encode(strings[1], "UTF-8"));
input.append(URLEncoder.encode("&", "UTF-8"));
input.append(URLEncoder.encode("date", "UTF-8"));
input.append(URLEncoder.encode("=", "UTF-8"));
input.append(URLEncoder.encode(strings[2], "UTF-8"));
input.append(URLEncoder.encode("&", "UTF-8"));
input.append(URLEncoder.encode("time", "UTF-8"));
input.append(URLEncoder.encode("=", "UTF-8"));
input.append(URLEncoder.encode(strings[3], "UTF-8"));
input.append(URLEncoder.encode("&", "UTF-8"));
input.append(URLEncoder.encode("city", "UTF-8"));
input.append(URLEncoder.encode("=", "UTF-8"));
input.append(URLEncoder.encode(strings[4], "UTF-8"));
String i = input.toString();
writer.write(i);
writer.flush();
writer.close();
os.close();
int response = con.getResponseCode();
if(response == HttpURLConnection.HTTP_OK){
BufferedReader r = new BufferedReader(new InputStreamReader(con.getInputStream()));
res = r.readLine();
}
} catch (Exception e) {
e.printStackTrace();
res = "error";
}
return res;
}
}
DBupdate obj = new DBupdate();
obj.execute(balance, hotcode, date, time, city);
}