POSTing android JSONObject problem for PHP

I am having trouble getting the android POST request for a simple JSONObject that will display in $ _POST data on the server. The PHP server is 5.3.4, and the android side is the SDK 8 emulator. I can publish a simple NameValuePair as usual and it appears, but when I switch to JSONObject + StringEntity, which you see below, the $ _POST array shows {}. Go ahead and run the code below on my test php page. It has var_dump $ _POST and $ _SERVER, and also searches for one of the expected keys ("email"). You will see that I tried a lot of "ContentType" to make sure this is a problem. I even used WireShark to verify that the TCP conversation looks good between the client and server. POST data is there, but not displayed on the servers. I'm stuck ... thanks for any help,which you can offer.

import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.json.JSONObject;

import android.util.Log;

public class TestPOST {
    protected static void sendJson (final String email, final String pwd) {
        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
        HttpResponse response;
        String URL = "http://web-billings.com/testPost.php";
        try{
            HttpPost post = new HttpPost(URL);

            // NameValuePair That is working fine...
            //List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
            //nameValuePairs.add(new BasicNameValuePair("email", email));  
            //nameValuePairs.add(new BasicNameValuePair("password", pwd));  
            //post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            //Log.i("main", "P2DB - String entity 'se' = "+nameValuePairs.toString());

            JSONObject jObject = new JSONObject();
            jObject.put("email", email);
            jObject.put("password", pwd);
            StringEntity se = new StringEntity(jObject.toString());
            //se.setContentType("charset=UTF-8");
            se.setContentType("application/json;charset=UTF-8");
            //se.setContentType("application/json");
            //se.setContentType("application/x-www-form-urlencoded");

            post.setEntity(se);
            Log.i("main", "TestPOST - String entity 'se' = "+GetInvoices.convertStreamToString(se.getContent()));

            response = client.execute(post);  

            /*Checking response */
            if(response!=null){
                InputStream in = response.getEntity().getContent(); //Get the data in the entity
                String message = GetInvoices.convertStreamToString(in);
                Log.i("main", "P2DB - Connect response = "+message);
            }
        }
        catch(Exception e){
            e.printStackTrace();
            //createDialog("Error", "Cannot Establish Connection");
        }
    }
}

testPost.php, :

<?php
    echo "\r\n<pre>\r\n";
    var_dump("\$_POST = ", $_POST)."\r\n";
    echo '$_POST[\'email\'] = '.$_POST['email']."\r\n";
    var_dump("\$_SERVER = ", $_SERVER)."\r\n";
    echo '</pre>';
    die; 
?>  
+3
3

, : 1) / Android, 2) PHP. - . JSONObject , , , , , , :

public class TestPOST2 {
    protected static void sendJson (final JSONObject json) {
        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
        HttpResponse response;
        String URL = "http://web-billings.com/testPost.php";

        try{
            HttpPost post = new HttpPost(URL);

            // Create a NameValuePair out of the JSONObject + a name
            List<NameValuePair> nVP = new ArrayList<NameValuePair>(2);  
            nVP.add(new BasicNameValuePair("json", json.toString()));  

            // Hand the NVP to the POST
            post.setEntity(new UrlEncodedFormEntity(nVP));
            Log.i("main", "TestPOST - nVP = "+nVP.toString());

            // Collect the response
            response = client.execute(post);  

            /*Checking response */
            if(response!=null){
                InputStream in = response.getEntity().getContent(); //Get the data in the entity
            }
        }
        catch(Exception e){
            e.printStackTrace();
            //createDialog("Error", "Cannot Establish Connection");
        }
    }
}
+4

, , HttpPost.setEntity - /, . $_POST , , -/. : , , .

Android/Java, :

HttpClient httpclient = new DefaultHttpClient();  
HttpPost httppost = new HttpPost("http://web-billings.com/testPost.php");  

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
nameValuePairs.add(new BasicNameValuePair("jsondata", se));  
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

PHP:

$json = file_get_contents('php://input');
+8

You send json string as one big value of post variable. This way, you will need to grab the json string on the server and convert it to an object before you can access the data in json from PHP.

$jsonString = file_get_contents('php://input');
$jsonObj = json_decode($jsonString, true);

if( !empty($jsonObj)) { 
    try {
        $email = $jsonObj['email'];
        $password = $jsonObj['password'];
    }
}
+4
source

Source: https://habr.com/ru/post/1795635/


All Articles