简体   繁体   中英

Unable to send JSON data from Android to ROR server

I am trying to send JSON data from my Android application to server. The database is MySQL and ROR is used for server side code. Below is the code used for sending 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 machine IP address. While debugging on eclipse I could see values in 'request' but it's giving error in while executing last HttpResponse response = client.execute(request); . I am very new to this so not sure if I a missing something. Also I am trying to check in rails server if any request is received. Nothing got initiated there. Please advise. Thanks.

send your json object in BasicNameValuePair //here sear ,shortvalue are json

 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();
                }

I think the problem is in the URL. Please try changing the

http://192.168.1.9/3000/categories/create

to:

http://192.168.1.9:3000/categories/create

Hope that helps,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM