1

I have seen many tutorials and questions using the following method to send JSON object to PHP from Android. For example this wordpress blog, this codeproject tutorial and some answers on stackoverflow like these.

All of these tutorials and answers use HTTP header to send data(body) to PHP like this.

....
// Post the data:
httppost.setHeader("json",json.toString());
....

As a programmer we all know that headers are not meant to carry data(body). Headers should only carry metadata.

So, is there a correct way to send JSON data to PHP from Android which does not involve setting data in header?

Community
  • 1
  • 1
aBhijit
  • 4,524
  • 8
  • 32
  • 53

2 Answers2

6

If you use nativ lib without Volley, here is dummy with HttpClient:

httpClient = createHttpClient();

//You wanna use POST method.
mPost = new HttpPost(_urlStr);

//Head
mPost.addHeader(new BasicHeader("Content-Type", "application/json"));

//Body
((HttpPost) mPost).setEntity(new StringEntity(jsonText));

//Do it.
client.execute(mPost);

Try to use Volley: https://github.com/ogrebgr/android_volley_examples/blob/master/src/com/github/volley_examples/Act_SimpleRequest.java

TeeTracker
  • 5,958
  • 4
  • 33
  • 42
0

Here is a simple tutorial to send and receive JSON objects in Android.

Ashish P.
  • 830
  • 5
  • 12