我正在尝试在 MYSQL 数据库中插入一个新行(以在该数据库中发布数据)
这是我的主要课程:
try {
JSONObject json = new JSONObject();
json.put("latitude", "4332314");
json.put("longitude", "1234567");
json.put("description", "Hahalela");
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
//
//String url = "http://10.0.2.2:8080/sample1/webservice2.php?" +
// "json={\"UserName\":1,\"FullName\":2}";
String url = "http://localhost/writemysql.php";
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes(
"UTF8")));
request.setHeader("json", json.toString());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
if (entity != null) {
InputStream instream = entity.getContent();
String result = RestClient.convertStreamToString(instream);
Log.i("Read from server", result);
// Toast.makeText(this, result,
// Toast.LENGTH_LONG).show();
}
} catch (Throwable t) {
// Toast.makeText(this, "Request failed: " + t.toString(),
// Toast.LENGTH_LONG).show();
}
这是我的 RestClient 数据:
public class RestClient {
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static void connect(String url)
{
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
Log.i("Praeda",response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
Log.i("Praeda",result);
// A Simple JSONObject Creation
JSONObject json=new JSONObject(result);
Log.i("Praeda","<jsonobject>\n"+json.toString()+"\n</jsonobject>");
// A Simple JSONObject Parsing
JSONArray nameArray=json.names();
JSONArray valArray=json.toJSONArray(nameArray);
for(int i=0;i<valArray.length();i++)
{
Log.i("Praeda","<jsonname"+i+">\n"+nameArray.getString(i)+"\n</jsonname"+i+">\n"
+"<jsonvalue"+i+">\n"+valArray.getString(i)+"\n</jsonvalue"+i+">");
}
// A Simple JSONObject Value Pushing
json.put("sample key", "sample value");
Log.i("Praeda","<jsonobject>\n"+json.toString()+"\n</jsonobject>");
// Closing the input stream will trigger connection release
instream.close();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
这是我的 php 脚本:
<?php
$con = mysql_connect("localhost","root","mypassword");
$d = mysql_select_db("radars");
if ($json = file_get_contents('php://input')) {
// Never mind. We'll do it ourselves.
$a = json_decode($json, true); // Now we have a nice PHP array.
$insert = '';
foreach ($a as $v) {
if ($insert) $insert .= ',';
$insert .= ' ("' . mysql_real_escape_string($v['latitude']) . '", "' . mysql_real_escape_string($v['longitude']) . '", "' . mysql_real_escape_string($v['description']) . '")';
$sql = 'INSERT INTO `radars` (`latitude`, `longitude`, `description`) VALUES' . $insert;
mysql_query($sql);
}
}
?>
运行应用程序后,我在 logcat 中收到这两个:
05-11 19:52:29.273: E/Buffer Error(532): Error converting result java.lang.NullPointerException
05-11 19:52:29.294: E/JSON Parser(532): Error parsing data org.json.JSONException: End of input at character 0 of
有谁知道问题是什么,或者我应该改变什么才能通过 JSON 向 MYSQL 发送数据?欢迎任何提示或建议。谢谢 !