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.

8
  • what does var_dump($data) prints? Commented Jun 2, 2011 at 7:38
  • This could mean that you don't have 'special php.ini directives' as hinted in the php manual (see my answer). I am not sure what these special directives are. I recommend trying the alternate method I have suggested Commented Jun 2, 2011 at 8:17
  • Yah I have tried your suggestiong but stil not working..of u know the best tutorial regarding this..Please provide me the link... Commented Jun 2, 2011 at 8:55
  • Can you show the updated code after trying my suggestion? Commented Jun 2, 2011 at 9:51
  • List<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("jsonParam", json.toString())); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters); formEntity.setContentEncoding("UTF-8"); formEntity.setContentType("multipart/form-data"); httppost.setEntity(formEntity); HttpResponse response = httpclient.execute(httppost); Commented Jun 2, 2011 at 9:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

<?php echo $_POST["jsonParam"]; ?> when i try this...error is: UNDEFINED Index

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.