Post data in MYSQL using JSON

SypeInfeffink

New Member
I am trying to insert a new row in MYSQL database(to post data in that database)Here is my main class:\[code\]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(); }\[/code\]and here is my RestClient data :\[code\]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 objectHttpGet httpget = new HttpGet(url); // Execute the requestHttpResponse 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();} }}\[/code\]Here is my php script : \[code\]<?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);}}?>\[/code\]After I run the application I receive this two in my logcat : \[code\] 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 \[/code\]Does anyone know what is the problem or what should I change to work sending data to MYSQL via JSON ? Any tips or suggestions are welcomed. Thank you !
 
Back
Top