Im learning webservices in Android, I made a simple app, Im sending a text from android to mysql . I have written code for this . Works fine, But the value which has to be stored in the table repeates 4 times. Couldn't figure out why? Please i need your help. Here's my code. MYSQL contains just 2 columns CITY_ID and CITY_NAME
public class Insert extends Activity {
String[] ct_name = null;
Button button;
InputStream is = null;
EditText name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText) findViewById(R.id.textname);
button = (Button) findViewById(R.id.textbutton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String textn = name.getText().toString();
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("c_name",textn));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/city1.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
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());
}
}
});
// http post
}
}
And php code goes as follows
<?php
$hostname_localhost ="localhost";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost);
$query="INSERT INTO CITY (`CITY_NAME`)VALUES('".$_POST['c_name']."')";
$exec=mysql_query($query);
if(!$exec)
echo "Error in query: ".mysql_error();
mysql_close();
?>
This is the error thats shown in the ddms
Error in http connectionorg.apache.http.NoHttpResponseException: The target server failed to respond