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 import arcpy出现importing _arcgisscripting 找不到相关程序
    • ¥15 onvif+openssl,vs2022编译openssl64
    • ¥15 iOS 自定义输入法-第三方输入法
    • ¥15 很想要一个很好的答案或提示
    • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
    • ¥15 怀疑手机被监控,请问怎么解决和防止
    • ¥15 Qt下使用tcp获取数据的详细操作
    • ¥15 idea右下角设置编码是灰色的
    • ¥15 全志H618ROM新增分区