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

  • SEARCH
  • Home
  • 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 5981577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:57:48+00:00 2026-05-22T21:57:48+00:00

I want to sent the data from an Android device to PHP. I have

  • 0

I want to sent the data from an Android device to PHP. I have tried but where I am wrong I don’t know. How do I correct this?

This is the Android code that I have done.

public class php_connect extends Activity {
    InputStream is;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        JSONObject json = new JSONObject();
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/food/food.php");
            json.put("id",1);
            json.put("name","john");
            Log.i("jason Object", json.toString());
            httppost.setHeader("json", json.toString());

            StringEntity se = new StringEntity(json.toString());
            se.setContentEncoding("UTF-8");
            se.setContentType("application/json");
            httppost.setEntity(se);
            HttpResponse response = httpclient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            Toast.makeText(getApplicationContext(),String.valueOf(statusCode), Toast.LENGTH_SHORT).show();
            is = se.getContent();
            Log.e("log_tag", "connection success ");
            Toast.makeText(getApplicationContext(), "Successfully Connected", Toast.LENGTH_SHORT).show();
        }
        catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
            Toast.makeText(getApplicationContext(), "Fail to Connect", Toast.LENGTH_SHORT).show();
        }
    }
}

I get response as 200 and connected successfully but cannot see data in PHP.

And my PHP code is:

<?php
    $data = file_get_contents('php://input');
    $json = json_decode($data,true);
    var_dump($json);
    $id=$json['id'];
    echo $id;
?>

I always get the null result.

  • 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-22T21:57:48+00:00Added an answer on May 22, 2026 at 9:57 pm

    From the PHP manual:

    In case of POST requests, it preferrable to $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype=”multipart/form-data”

    I think it will be even easier to encode your JSON as a form parameter and use the normal PHP $_POST function to read it:

    List parameters = new ArrayList();
    parameters.add(new BasicNameValuePair("jsonParam", json.toString())
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters)
    httppost.setContentType("multipart/form-data");
    httppost.setEntity(formEntity);   
    

    On the PHP side you can simply do

    Got <?php echo $_POST["jsonParam"]; ?>!<br />
    

    Note: I have not tested the above code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a quick question. I want to send data from Javascript to a PHP
We have a production system that gathers telemetry data from remote devices. This data
I want to be a able to securely send data from my Android App
i am getting json data from this link now i want data from html_instructions:
I am using FCKEditor with CakePHP and when I save data sent from the
I have a contact form i want to use Ajax for. My contact.php script
I want to transmit data from a Queue using Twisted. I currently use a
If all tables I want to delete from have the column gamer_id can i
I have added the library ksoap2-android-assembly-2.4-jar-with-dependencies.jar but i am getting below error : Exception
I want to pull data from table Province_Notifiers and also fetch all corresponding items

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.