我在Android学习webservices,我做了一个简单的应用程序,我发送一个文本从android到mysql。我已为此编写代码。工作正常,但必须存储在表中的值重复4次。想不明白为什么?拜托我需要你的帮忙。这是我的代码。 MYSQL只包含两列CITY_ID和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
}
}
php代码如下
<?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();
?>
这是ddms
中显示的错误http connectionorg.apache.http.NoHttpResponseException中的错误: 目标服务器无法响应