无法从 android 客户端到 php 服务器 post 变量
Cannot post variable from android client to php server
我开发了一个 android 代码用于在 android 应用程序和 PHP 之间传输和接收。基于 JSON 的接收部分正常工作。我已经在 PHP 代码中通过手动设置变量进行了测试。但是,当我将变量从 android 发布到 php 时,它无法接收它。谁能告诉我问题所在?
您评论了这一行,这意味着您没有传递来自 Android
的值
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
删除该行的注释。
另外一件事,您正在传递 username
,但您正试图从 php 获取值作为 $user = $_POST['name'];
,两个名称必须相同。
@Override
protected String doInBackground(Void... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", <Your username here>));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(<Your URL to php file>);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = httpclient.execute(httppost); // Execute Post to URL
String st = EntityUtils.toString(response.getEntity()); // This is the result from php web
Log.d(TK_Configuration.TAG, "In the try Loop" + st); // Still executing
finalResult = st; // You should register a variable for finalResult;
} catch (Exception e) {
Log.d(TK_Configuration.TAG, "Connection error : " + e.toString());
}
return "OK";
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
// After that, you will have final result and process to do with it here
// Below is my simple code, please change it
if(finalResult.equals("1")){
Toast.makeText(context, context.getResources().getString(R.string.upload_bike_success), Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, context.getResources().getString(R.string.upload_bike_fail), Toast.LENGTH_SHORT).show();
}
// End
}
请试试这个,还有一点,你应该使用 Gson 库在从服务器获得 JSON 字符串后快速将 JSON 解码为 Java 对象。
注意:将 TK_Configuration.TAG << 替换为您的 TAG。
我开发了一个 android 代码用于在 android 应用程序和 PHP 之间传输和接收。基于 JSON 的接收部分正常工作。我已经在 PHP 代码中通过手动设置变量进行了测试。但是,当我将变量从 android 发布到 php 时,它无法接收它。谁能告诉我问题所在?
您评论了这一行,这意味着您没有传递来自 Android
的值httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
删除该行的注释。
另外一件事,您正在传递 username
,但您正试图从 php 获取值作为 $user = $_POST['name'];
,两个名称必须相同。
@Override
protected String doInBackground(Void... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", <Your username here>));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(<Your URL to php file>);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = httpclient.execute(httppost); // Execute Post to URL
String st = EntityUtils.toString(response.getEntity()); // This is the result from php web
Log.d(TK_Configuration.TAG, "In the try Loop" + st); // Still executing
finalResult = st; // You should register a variable for finalResult;
} catch (Exception e) {
Log.d(TK_Configuration.TAG, "Connection error : " + e.toString());
}
return "OK";
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
// After that, you will have final result and process to do with it here
// Below is my simple code, please change it
if(finalResult.equals("1")){
Toast.makeText(context, context.getResources().getString(R.string.upload_bike_success), Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, context.getResources().getString(R.string.upload_bike_fail), Toast.LENGTH_SHORT).show();
}
// End
}
请试试这个,还有一点,你应该使用 Gson 库在从服务器获得 JSON 字符串后快速将 JSON 解码为 Java 对象。
注意:将 TK_Configuration.TAG << 替换为您的 TAG。