[英]PHP Post array empty
目前無法訪問服務器端腳本的$ _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數組為空。 我意識到這可能有點模糊,但我希望你理解。 我正在為兩種方法執行完全相同的發送數據的過程,但后者是空的。
你有舊的==
vs equals
問題,加上String
interning。
這是罪魁禍首
if(method == "POST"){
應該
if(method.equals("POST")){
第一種方法適用於普通的String對象。 任何"POST"
的實例結果真實,比其他任何時候"POST"
實例(查找字符串實習來了解為什么)。 然而在后一種情況下,你有一些有趣的東西:
JSONObject jsonEdit = jsonParser.makeHttpRequest(url_edit,
"POST" + //Ooooops, what is this?
"", params);
現在你比較"POST"
== "POST"+""
......這不是按內容比較它們,而是通過參考! 而第二個是不同的,導致比較false
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.