1

I've tried several things but my android app is not sending post parameters. I run the app on a virtual device. This is the code:

@Override
public void run() {
    try{
        HttpClient client = new DefaultHttpClient();  
        HttpPost post = new HttpPost(page);   

        HttpParams httpParams = client.getParams();
        httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);

        post.setHeader("Content-type", "application/json");
        post.setHeader("Accept", "application/json");
        JSONObject obj = new JSONObject();
        obj.put("username", "abcd");
        obj.put("password", "1234");
        post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
        HttpResponse response = client.execute(post);
        InputStreamReader isr = new InputStreamReader(response.getEntity().getContent());
        BufferedReader reader = new BufferedReader(isr);
        String line = "";
        while((line = reader.readLine()) != null){
            System.out.println(line);
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}

It should send a post request to a PHP page. This page displays the output of the POST array:

<?php
print_r($_POST);
?>

When I run the app, it displays an empty array.

3 Answers 3

1

thats because you're sending JSON

standard php $_POST is build from key-value pairs so you should post key1=value1&key2=value2

or you should read from

$HTTP_RAW_POST_DATA

or

<?php $postdata = file_get_contents("php://input"); ?> 

and use

json_decode( $postdata );

PHP will not automatically decode json for you

you can also use another approach and POST your json like data=YourJsonCode

and then decode it using json_decode( $_POST['data'] );

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

6 Comments

When I do: var_dump(json_decode($postdata)); it returns NULL
Nope is displays nothing :( But i've also tried other ways to send a request with post parameters without json and also won't work.
like post.setEntity(new StringEntity("key1=value1", "UTF-8")); ?
in that case you should probably change content type to application/x-www-form-urlencoded
Tried the other way you said and changed the content type but the $_POST is an empty array and the $postdata (php://input) is empty.
|
0

Try sending url encoded name/value pairs. You can also use EntityUtils to convert the response to a String for you.

HttpClient client = new DefaultHttpClient();  
HttpPost post = new HttpPost(page);

HttpParams httpParams = client.getParams();
httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);

post.setHeader("Content-Type","application/x-www-form-urlencoded");

List<NameValuePair> formParams = new ArrayList<NameValuePair>();
formParams.add(new BasicNameValuePair("username", "abcd"));
formParams.add(new BasicNameValuePair("password", "1234"));

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams,HTTP.UTF_8);
post.setEntity(entity);
HttpResponse httpResponse = client.execute(post);
System.out.println(EntityUtils.toString(httpResponse.getEntity()));

1 Comment

THanks for your answer. But it isn't working. The $_POST array is empty and $_SERVER['REQUEST_METHOD'] returns GET
0

Problem solved. There was a htaccess file that redirected all non www pages.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.