我正在尝试使用Android应用程序在我的数据库mySql中插入一个值。
这是从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 + "\n");
}
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();
}
}
这里是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);
?>
包含文件我确定它有效。究竟出现的是带有此日志的崩溃onClick按钮:
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)
我找不到解决方案..我不是php中的大师,所以也许是insert.php的问题。感谢