简体   繁体   中英

PHP Post array empty

Currently having trouble accessing some variables in the $_POST array of a server side script.

Context: I'm submitting some values to a mySQL server from an android device using PHP. When i "Create a new route" the php and java is working fine, for example:

Building a list of variables to post:

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));

I then call my method to post them:

        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();

And this works fine and the values are available to the PHP script in the $_POST array. The problem i'm having is when I do the exact same process, using the following (Note in this case i'm "updating a route" and so there are a few less variables, this is reflected in the PHP script for updating a route.:

            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);

The $_POST array is empty on the php script server side. I realise this may be a little vague but i hope you understand. i'm doing the exact same process of sending data for both methods, however the latter is empty.

You have the old == vs equals issue, spiced with String interning.

This is the culprit

if(method == "POST"){

Should be

if(method.equals("POST")){

The first method works with plain String objects. Any "POST" instance results in true, when compared to any other "POST" instance (look up String interning to get to know why). However in the latter case you have something funny in there:

JSONObject jsonEdit = jsonParser.makeHttpRequest(url_edit,
                "POST" + //Ooooops, what is this?
                "", params);

Now you compare "POST" == "POST"+"" ... This does not compare them by content, but by reference! And the second one is a different one, resulting in a false from the comparison...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM