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.?
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?
$_REQUEST
is not always supported, use $_GET
or $_POST
depending on where your parameter lives.
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();
}
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?