douqin6785 2014-10-21 20:33
浏览 50

无法从Android应用程序向mysql插入值

I'm trying to insert a value in my database mySql using an android application.

this is the method to insert value from android:

private void insert() {
        // TODO Auto-generated method stub
        ArrayList<NameValuePair> namePairs = new ArrayList<NameValuePair>();
        namePairs.add(new BasicNameValuePair("titolo", titoloTxt));
        namePairs.add(new BasicNameValuePair("content", contentTxt));

        try {


            // HttpClient per avviare la connessione
            HttpClient client = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://www.example.net/connect/insert.php");
            httpPost.setEntity(new UrlEncodedFormEntity(namePairs));
            HttpResponse response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();

            is = entity.getContent();
            Log.i("Db connect", "Success");


        } catch (Exception e) {
            e.printStackTrace(); 
            Log.e("Errore connessione", e.toString());
            Toast.makeText(getApplicationContext(), "IP non valido", Toast.LENGTH_SHORT).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 + "
");
            }
            is.close();
            result = sb.toString();
            Log.i("Connection", "Connessione eseguita con successo");
        } catch (Exception e) {
            e.printStackTrace();
        }

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

            if(code==1){
                Toast.makeText(getBaseContext(), "Insert ok", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getBaseContext(), "Non va na pippa", Toast.LENGTH_SHORT).show();
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

and here the insert.php EDITED

<?php

include ('db_connection.php');

$id=$_POST['ID'];
$title=$_POST['post_title'];
$content=$_POST['post_content'];

$flag['code']=0;

$sql_insert_post="INSERT INTO wp_posts (ID, post_title, post_content)
VALUES ('$id','$title','$content')";

if (!mysqli_query($db,$sql_insert_post)) {
    die('Error: ' . mysqli_error($db));
}
    $flag['code']=1;
    echo "Hi";

print(json_encode($flag));
mysqli_close($db);
?>

the include file i'm sure it works. what exactly appear is a crash onClick button with this log:

10-21 22:28:10.851: E/AndroidRuntime(1785): FATAL EXCEPTION: main
10-21 22:28:10.851: E/AndroidRuntime(1785): Process: com.dbconnect.dbconnect, PID: 1785
10-21 22:28:10.851: E/AndroidRuntime(1785): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
10-21 22:28:10.851: E/AndroidRuntime(1785):     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at org.json.JSONObject.<init>(JSONObject.java:156)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at org.json.JSONObject.<init>(JSONObject.java:173)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at com.dbconnect.dbconnect.MainActivity.insert(MainActivity.java:110)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at com.dbconnect.dbconnect.MainActivity.access$2(MainActivity.java:67)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at com.dbconnect.dbconnect.MainActivity$1.onClick(MainActivity.java:63)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at android.view.View.performClick(View.java:4756)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at android.view.View$PerformClick.run(View.java:19749)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at android.os.Handler.handleCallback(Handler.java:739)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at android.os.Handler.dispatchMessage(Handler.java:95)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at android.os.Looper.loop(Looper.java:135)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at android.app.ActivityThread.main(ActivityThread.java:5221)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at java.lang.reflect.Method.invoke(Native Method)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at java.lang.reflect.Method.invoke(Method.java:372)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
10-21 22:28:10.851: E/AndroidRuntime(1785):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

i can't find the solution.. i'm not a guru in php so maybe it's the insert.php the problem. Thanks

  • 写回答

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时遇到的编译问题