dongxiao3694 2012-03-03 17:32
浏览 38

在MySql:Simple Http Post中从android重复发送数据

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题