The problem of publishing Android jsonobject to PHP

I can't get a simple Android post of jsonobject to display on the server$_ In the post data, the server is PHP 5.3.4 and the Android side is SDK 8 simulator. I can release a simple namevaluepair as usual, but when I switch to the jsonobject stringentity you see below$_ The post array shows {}. Continue to run the following code on my test PHP page. It has a$_ Post and$_ VaR of server_ Dump, and search for one of the expected keys ('email '). You will see that I have tried a lot of' contenttype 'to see if it is a problem. I even use Wireshark to verify whether the TCP session between the client and the server is normal. The post data is there, but it is not displayed in the server variable. I am trapped... Thank you for any help

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

If you like, this is the testpost.php page:

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

resolvent:

From what I can see, httppost.setentity sets the subject of the request. There is no name / value pairing, but the original post data$_ Post does not look for raw data, only name value pairs. It will be converted to hash table / array. You have two choices... Either process the raw published data or format the request to include name value pairs

Android / Java, name value pair example:

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

Raw post data access in PHP:

$json = file_get_contents('PHP://input');

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>