目前无法访问服务器端脚本的$ _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脚本可以使用这些值。 我遇到的问题是,当我执行完全相同的过程时,使用以下(注意,在这种情况下,我是“更新路径”,因此有一些较少的变量,这反映在用于更新a的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);
PHP脚本服务器端的$ _POST数组为空。我意识到这可能有点模糊,但我希望你理解。我正在为两种方法执行完全相同的发送数据的过程,但后者是空的。
答案 0 :(得分:2)
您有==
vs equals
旧问题,加上String
实习。
这是罪魁祸首
if(method == "POST"){
应该是
if(method.equals("POST")){
第一种方法适用于普通的String对象。与任何其他"POST"
实例相比,任何"POST"
实例都会生成true(查找String interning以了解原因)。然而在后一种情况下,你有一些有趣的东西:
JSONObject jsonEdit = jsonParser.makeHttpRequest(url_edit,
"POST" + //Ooooops, what is this?
"", params);
现在您比较"POST"
== "POST"+""
...这不按内容比较它们,但是通过引用!第二个是另一个,从比较中得到false
......