从Android应用程序插入数据到mysql数据库

时间:2014-05-17 10:36:44

标签: php android mysql

xxx我正在尝试使用php文件从我的Android应用程序中插入mysql数据库中的数据。我不知道我做错了什么,这是我的android代码:

public void insert()
    {
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


        nameValuePairs.add(new BasicNameValuePair("nick",nick));
        nameValuePairs.add(new BasicNameValuePair("minuto",minuto));
        nameValuePairs.add(new  BasicNameValuePair("id_pelicula",String.valueOf(id_pelicula)));

        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://xxxx.xxxxx.com/xxxx/insertar.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost); 
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("pass 1", "connection success ");
        }
        catch(Exception e)
        {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
            Toast.LENGTH_LONG).show();
        }     

        try
        {
            BufferedReader reader = new BufferedReader
            (new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
        {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        Log.e("pass 2", "connection success ");
    }
        catch(Exception e)
    {
            Log.e("Fail 2", e.toString());
    }     

    try
    {
            JSONObject json_data = new JSONObject(result);
            code=(json_data.getInt("code"));

            if(code==1)
            {
            Toast.makeText(getBaseContext(), "Opinión enviada correctamente",
            Toast.LENGTH_SHORT).show();
            }
            else
            {
            Toast.makeText(getBaseContext(), "Disculpe, inténtelo de nuevo",
            Toast.LENGTH_LONG).show();
            }
    }
    catch(Exception e)
    {
            Log.e("Fail 3", e.toString());
    }
    }

这是我的php文件代码:

<?php

    // Connection data
        $host = 'xxx.xxx.com';;
        $uname = 'uxxxx';
        $pwd = 'xxxxx';
        $db = 'xxxxx';


        $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
        mysql_select_db($db,$con) or die("db selection failed");

        $nick=$_REQUEST['nick'];
        $minuto=$_REQUEST['minuto'];
        $id_pelicula=$_REQUEST['id_pelicula'];

        $flag['code']=0;

        if($r=mysql_query("insert into opiniones values('$nick','$minuto','$id_pelicula') ",$con))
        {
            $flag['code']=1;
            echo"hi";
        }

        print(json_encode($flag));
        mysql_close($con);
    ?>

也许是因为在我的mysql数据库中,“minuto”变量是整数,我使用的是来自android的String?请有人可以帮帮我吗?非常感谢你

1 个答案:

答案 0 :(得分:0)

我已经解决了,我在这句话中的php文件中出错:

if($r=mysql_query("insert into opiniones (nick, minuto, id_pelicula) values('$nick','$minuto','$id_pelicula') ",$con))