I used following code in java class of android. Everything works except it wont send any request. Answer is always null.
public class WebviewActivity extends Activity {
/** Called when the activity is first created. */
EditText et; Editable ur;
TextView tx;HttpResponse response;String x;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tx=(TextView)findViewById(R.id.textView1);
et=(EditText)findViewById(R.id.editText1);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(send);
}
private OnClickListener send = new OnClickListener() {
public void onClick(View v) {
ur=et.getText();
postData();
tx.setText("ans:"+x);
}};
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/name.php");
try {
// Add your data
List nameValuePairs = new ArrayList(1);
nameValuePairs.add(new BasicNameValuePair("name", ur.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
Log.d("myapp", "response " + response.getEntity());
x= EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }
and in php i used following code
$name=$_POST["name"];
echo $name;
?>
Any idea how it make it work?