1

This is for an Android application ,where my mobile developer is trying to post json data to my PHP page .

Below is the function that is being used :

public static String postData(String url, String postData) {
  // Create a new HttpClient and Post Header
     InputStream is = null;
     StringBuilder sb = null;
     String result = "";

//   StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//   StrictMode.setThreadPolicy(policy);
     HttpClient httpclient = new DefaultHttpClient();

     HttpPost httppost = new HttpPost(url);

     try {
         httppost.setEntity(new StringEntity(postData));
         httppost.setHeader("Accept", "application/json");
         httppost.setHeader("Content-type", "application/json");
         // Execute HTTP Post Request
         HttpResponse response = httpclient.execute(httppost);
         HttpEntity entity = response.getEntity();
         is = entity.getContent();

     } catch (Exception e) {
         Log.e("log_tag", "Error in http connection" + e.toString());
         //throw new CustomException("Could not establish network connection");
     }
        try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "utf-8"), 8);

                sb = new StringBuilder();
                sb.append(reader.readLine() + "\n");
                String line = "0";

                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();

        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
            //throw new CustomException("Error parsing the response");
        }
  return result;

 }

Where url is the link to my php webpage . On my php page , i am just trying to print out the posted data by doing :

print_r($_POST);

But it shows an empty array . I even tried using the REST addon for firefox and doing the same but it simply shows a blank array .

Would be great if someone could point out if i am missing anything .

Thanks.

0

1 Answer 1

1

you need to get the json content like below :

if(isset($_POST))
    {
     $json = file_get_contents('php://input');
     $jsonObj = json_decode($json);
     echo $jsonObj;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.