Failed to send Android JSON data to ROR server

I am trying to send JSON data from my Android application to the server. The database is MySQL and ROR is used for server-side code. Below is the code used to send data.

try{ JSONObject json = new JSONObject(); json.put("id", "1"); json.put("catname", "gaurav"); json.put("catstart", "01012013"); json.put("catend", "01012013"); json.put("catvisible", "Y"); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 5000); HttpConnectionParams.setSoTimeout(httpParams, 5000); HttpClient client = new DefaultHttpClient(httpParams); String url = "http://192.168.1.9/3000/categories/create"; HttpPost request = new HttpPost(url); request.addHeader("Accept","application/json"); request.addHeader("Content-Type","application/json"); request.setEntity(new ByteArrayEntity(json.toString().getBytes( "UTF8"))); //request.setHeader("json", json.toString()); HttpResponse response = client.execute(request); 

Here 192.168.1.9 is my computer IP address. During debugging in eclipse, I could see the values ​​in the "request", but it threw an error when executing the last HttpResponse response = client.execute(request); . I am very new to this, so I'm not sure if I missed something. I am also trying to check the rails server if any request is received. Nothing started there. Please advise. Thanks.

+4
source share
2 answers

I think the problem is in the url. Try to change

 http://192.168.1.9/3000/categories/create 

in

 http://192.168.1.9:3000/categories/create 

Hope this helps,

0
source

send your json object to BasicNameValuePair // here is a whisper, short json value

  DefaultHttpClient client1 = new DefaultHttpClient(); List<NameValuePair> params1 = new ArrayList<NameValuePair>(); params1.add(new BasicNameValuePair("q",sear)); params1.add(new BasicNameValuePair("o",shortvalue)); params1.add(new BasicNameValuePair("p","1")); params1.add(new BasicNameValuePair("filter_on",String.valueOf(jarry))); String paramString = URLEncodedUtils.format(params1, "utf-8"); HttpGet httpGet = new HttpGet(servername+"search/"+"?" + paramString); //httpGet.setHeader("Cookie","_bb_vid="+""+Vis_id12); try { httpResponse = client1.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); responseCode = httpResponse.getStatusLine().getStatusCode(); String line = null; BufferedReader reader1 = null; try { reader1 = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8")); } catch (Exception e) { // TODO: handle exception exp_Message="Search.java"+" "+e+" "+e.getMessage()+" "+Log.getStackTraceString(e); Writefile(); } while ((line = reader1.readLine()) != null) { try { filObject = new JSONObject(line); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); exp_Message="Search.java"+" "+e+" "+e.getMessage()+" "+Log.getStackTraceString(e); Writefile(); } catch (Exception e) { // TODO: handle exception exp_Message="Search.java"+" "+e+" "+e.getMessage()+" "+Log.getStackTraceString(e); Writefile(); } } searchJsonObject=filObject; checkfil=true; } catch (Exception e) { // TODO: handle exception exp_Message="Search.java"+" "+e+" "+e.getMessage()+" "+Log.getStackTraceString(e); Writefile(); } 
0
source

Source: https://habr.com/ru/post/1495458/


All Articles