这里我的代码,我需要插入mysql数据库我有mysql,php,android
1)我有问题,我需要一个成功的结果到我的PHP,以便我能够插入使用android.so我的代码为PHP:
2)我正在使用的表有一个外键:Provinces_idProvinces
。
$result = mysql_query("INSERT INTO property( Pname, P_Price,P_Desc,P_City, P_Size,P_Rooms, P_garage, P_Address, P_Long, P_Lat, Provinces_idProvinces)
VALUES('$_POST[Pname]',$_POST[P_Price],'$_POST[P_Desc]','$_POST[P_City]','$_POST[P_Size]','$_POST[P_Rooms]',$_POST[P_garage],'$_POST[P_Address]',$_POST[P_Long],$_POST[P_Lat],$_POST[Provinces_idProvinces])");
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = $result ;
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
echo $response["success"];
// echoing JSON response
echo json_encode($response);
}
我传递参数的类,但它给了我一个错误:
List<NameValuePair> params = new ArrayList<NameValuePair>();
Log.d("Parameters","1");
params.add(new BasicNameValuePair("Pname",Pname));
params.add(new BasicNameValuePair("P_Price",Prices));
params.add(new BasicNameValuePair("P_Desc",Desc));
params.add(new BasicNameValuePair("P_City",city));
params.add(new BasicNameValuePair("P_Size",Size));
params.add(new BasicNameValuePair("P_Rooms",bedNumber));
params.add(new BasicNameValuePair("P_garage",Garnums));
params.add(new BasicNameValuePair("P_Address",Address));
params.add(new BasicNameValuePair("P_Long",longertude));
params.add(new BasicNameValuePair("P_Lat",lattitude));
params.add(new BasicNameValuePair("Provinces_idProvinces",Provnums));
Log.d("Json","obj");
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(save_prop,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
我在JSON中收到错误,因为值为0,由php返回, 我不知道如何解决这个问题
这是我的logCat:
06-25 19:36:40.175: E/JSON Parser(2261): Error parsing data org.json.JSONException: Value 0 of type java.lang.Integer cannot be converted to JSONObject
06-25 19:36:40.175: W/dalvikvm(2261): threadid=10: thread exiting with uncaught exception (group=0x40014760)
06-25 19:36:40.236: E/AndroidRuntime(2261): FATAL EXCEPTION: AsyncTask #1
06-25 19:36:40.236: E/AndroidRuntime(2261): java.lang.RuntimeException: An error occured while executing doInBackground()
06-25 19:36:40.236: E/AndroidRuntime(2261): at android.os.AsyncTask$3.done(AsyncTask.java:266)
答案 0 :(得分:0)
Error parsing data org.json.JSONException: Value 0 of type java.lang.Integer cannot be converted to JSONObject
在代码中的某处,您尝试将整数值转换为JSONObject。
看来这段代码是罪魁祸首
JSONObject json = jsonParser.makeHttpRequest(save_prop,
"POST", params);
确保您的php响应是JSONObject而不是“Integer”。
答案 1 :(得分:0)
编辑:只需删除echo $response["success"];
。
你不应该像你拥有的那样打印出结果。我假设这是当您使用该请求点击服务器时结果会是什么样的......
0{"results":"..."}
您收到此错误,因为结果(如上所示)在Android端被用作JSON对象。上面的结果不是有效的JSON格式,因此抛出异常。所以我的建议是重做你的PHP以返回看起来像......的结果。
{"success":0,"results":"..."}
答案 2 :(得分:0)
我很抱歉我没有回答你的问题,但我注意到了 您的PHP代码容易受到SQL注入攻击,因为您没有在SQL查询中转义POST变量。更好的是:你应该使用准备好的声明。