Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to Android programming, but have decent knowledge of PHP.

I am trying to send data to my server via post request. I have the following code:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream inps = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inps));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
    sb.append(line);
}
inps.close();
result=sb.toString();
Log.v("data-received",result);

I have found the code at numerous places all over the Internet.

On the PHP side I am writing this:

<?php
    echo "something".$_REQUEST['year'];
?>

But I am only getting "something" (in the Log value "data-received") as the output at my app end?

What am I doing wrong? Do I need to set any environment variable, etc.?

share|improve this question
    
What is the content-type set to? Needs to be specific for PHP to acknowledge. –  lucifurious May 17 '13 at 15:31
    
I added content-type too..still not working... –  Shubham May 21 '13 at 4:35
    
Make sure your content-type is set to "application/x-www-form-urlencoded" and that your content-length is set as well. I don't know the Java API well enough to say if it's being set for you. In your server side script, do this: "print_r( $_SERVER ); print_r( $_POST );". That will dump the headers and post data to the screen so you can visually inspect the results. Let me know what you find out. –  lucifurious May 21 '13 at 19:50
    
Hey man, are you the Shubham who did the gem thing for DH4? ;) –  lucifurious May 21 '13 at 19:54

4 Answers 4

$_REQUEST is not always supported, use $_GET or $_POST depending on where your parameter lives.

share|improve this answer
    
I tried both, but still its not working –  Shubham May 17 '13 at 15:04
    
Can you log the request you're sending? Maybe you've made a mistake in constructing the request. –  Halcyon May 17 '13 at 15:05
    
I also tried print_r(var_dump($_REQUEST)), print_r(var_dump($_POST)) and print_r(var_dump($_GET))....its returns array(0){} –  Shubham May 17 '13 at 15:05
    
the data I am sending is the first 2 lines...do I need to send anything else along with it? –  Shubham May 17 '13 at 15:06
    
I mean, show the raw HTTP request, if you can. If the HTTP request is good then the problem is with the receiver. If the HTTP request is bad the problem with the sender. –  Halcyon May 17 '13 at 15:08

I use this peace of code to send date to a php file. This is using POST. I'm hoping you can do something with it...

URL url;
            try {
                url = new URL("url.nl/phppage.php");


              InputStream myInputStream =null;
                StringBuilder sb = new StringBuilder();
                        //adding some data to send along with the request to the server

    // Data below:  
    sb.append("email="+email.getText().toString()+"&name="+name.getText().toString()+"&message="+om.getText().toString());


                    //url = new URL(url);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setDoOutput(true);
                    conn.setRequestMethod("POST");
                    OutputStreamWriter wr = new OutputStreamWriter(conn
                            .getOutputStream());
                                // this is were we're adding post data to the request
                                wr.write(sb.toString());
                    wr.flush();
                    myInputStream = conn.getInputStream();
                    wr.close();
                    Context context = getApplicationContext();
                     Log.i("TAG", "Message send successful");
                CharSequence text = "Message send successful";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show(); 
                finish();
                } catch (Exception e) {
                      Context context = getApplicationContext();
                      Log.e("TAG", "Message not sended - SERVER POST ERROR");
                            CharSequence text = "Message couldn't be send.";
                            int duration = Toast.LENGTH_SHORT;

                            Toast toast = Toast.makeText(context, text, duration);
                            toast.show(); 


                }
share|improve this answer
    
what is getApplicationContext()? –  Shubham May 17 '13 at 16:07
    
@Shubham It's just an function to get a application context. It's sometime given in a function, but this is a workaround if it is not... –  Beko1997 May 17 '13 at 16:24
    
I tried this code...its still not working...is something wrong with the server? –  Shubham May 21 '13 at 4:09
    
Are you trying it in the Emulator? Because I remember I had problems with that too. –  Beko1997 May 21 '13 at 14:46
    
Yes..I am using Emulator on Mac... –  Shubham May 21 '13 at 18:50

In the code is already an check, he is giving a toast message and a log when successful. On the php server you can use print_r($_POST); This should be no problem. What is the code you're using right now?

share|improve this answer
    
I tried that...gave me an empty array...please check the solution...can you explain why it is happening? –  Shubham May 22 '13 at 15:46
    
What is the code you're using right now? Because the posted code is working for me. Isn't the problem with the data you're sending? –  Beko1997 May 23 '13 at 14:10
    
I wrote the same code except for the first line...as given in the solution... –  Shubham May 23 '13 at 17:24
up vote 0 down vote accepted

I finally got it to work. The only change is in the first line:

List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePairs>();

instead of

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

Does anybody have any idea why is it working?

share|improve this answer
    
maybe the type mismatch in the next line... –  Shubham May 22 '13 at 15:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.