当前无法访问服务器端脚本的 $_POST 数组中的某些变量。
上下文:我正在使用 PHP 从 Android 设备向 mySQL 服务器提交一些值。当我“创建新路线”时,php 和 java 工作正常,例如:
构建要发布的变量列表:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("startLat", startLat));
params.add(new BasicNameValuePair("startLong", startLong));
params.add(new BasicNameValuePair("endLat", endLat));
params.add(new BasicNameValuePair("endLong", endLong));
params.add(new BasicNameValuePair("waypoints", waypoints.toString()));
params.add(new BasicNameValuePair("rating", rating));
params.add(new BasicNameValuePair("difficulty", difficulty));
params.add(new BasicNameValuePair("enjoyability", enjoyability));
params.add(new BasicNameValuePair("count", count));
params.add(new BasicNameValuePair("sum", sum));
params.add(new BasicNameValuePair("countDiff", countDiff));
params.add(new BasicNameValuePair("sumDiff", sumDiff));
params.add(new BasicNameValuePair("countEnjoy", countEnjoy));
params.add(new BasicNameValuePair("sumEnjoy", sumEnjoy));
然后我调用我的方法来发布它们:
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//System.out.println(httpPost.getURI().toString());
//System.out.println(httpPost.getEntity().toString());
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
这可以正常工作,并且 $_POST 数组中的 PHP 脚本可以使用这些值。我遇到的问题是,当我使用以下方法执行完全相同的过程时(注意在这种情况下,我正在“更新路线”,因此变量较少,这反映在用于更新的 PHP 脚本中路线。:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("rating", Double.toString(currentRating)));
params.add(new BasicNameValuePair("difficulty", Double.toString(currentDiff)));
params.add(new BasicNameValuePair("enjoyability", Double.toString(currentEnjoy)));
params.add(new BasicNameValuePair("count", Double.toString(currentCount)));
params.add(new BasicNameValuePair("sum", Double.toString(currentSum)));
params.add(new BasicNameValuePair("countDiff", Double.toString(currentDiffCount)));
params.add(new BasicNameValuePair("sumDiff", Double.toString(currentDiffSum)));
params.add(new BasicNameValuePair("countEnjoy", Double.toString(currentEnjoyCount)));
params.add(new BasicNameValuePair("sumEnjoy", Double.toString(currentEnjoySum)));
JSONObject jsonEdit = jsonParser.makeHttpRequest(url_edit,
"POST" +
"", params);
$_POST 数组在 php 脚本服务器端为空。我意识到这可能有点模糊,但我希望你能理解。我正在为两种方法发送数据的过程完全相同,但是后者是空的。