Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4028454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:13:19+00:00 2026-05-20T11:13:19+00:00

I am having trouble getting an android POST of a simple JSONObject to show

  • 0

I am having trouble getting an android POST of a simple JSONObject to show up in the $_POST data on the server. The server is PHP 5.3.4 and the android side is an SDK 8 emulator. I can post a simple NameValuePair as normal and it shows up but when I switch to the JSONObject + StringEntity that you see below the $_POST array shows { }. Go ahead and run the code below against my test php page. It has a var_dump of $_POST and $_SERVER as well as searching for one of the expected keys (’email’). You will see I have tried numerous ‘ContentType’s to see if that was the problem. I’ve even used WireShark to verify that the TCP conversation looks good between client and server. The POST data is in there but it isn’t showing up in the server’s vars. I am stuck… thanks for any help 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");
        }
    }
}

Here is the testPost.php page if you like:

<?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; 
?>  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:13:20+00:00Added an answer on May 20, 2026 at 11:13 am

    Thanks much to Jeff Parker and Saurav for identifying the issue of either: 1) set a name/value pair on the android side, or 2) parse the raw input on the PHP side. Because of their advice here is a much cleaner and running version of the original code. I pass in a JSONObject in this boiled down copy because that is what I am doing in my real code and there are lots of things to do to make this really sea worthy but these are the basic working parts:

    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");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble getting the following to work in SQL Server 2k, but it
I'm having trouble getting in touch with SQL Server Managemen Studio 2008! I want
I am using HTML Purifier in my PHP project and am having trouble getting
I am having trouble getting the Android Development Tools to install. I overcame one
I am having trouble with with getting my application to function on android 1.5.
Im having trouble getting my head round subqueries in Mysql. Fairly simple ones are
I'm having trouble getting a rotary encoder to work properly with AVR micro controllers.
I've been having trouble getting my ASP.NET application to automatically log users into the
I'm having trouble getting the right number of elements in the ArrayList alt in
I am having trouble getting my ASP.NET application to start an application. For example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.