无法将JSON数据从Android发送到ROR服务器

时间:2013-08-06 06:50:30

标签: android ruby-on-rails json

我正在尝试将JSON数据从我的Android应用程序发送到服务器。数据库是MySQL,ROR用于服务器端代码。以下是用于发送数据的代码。

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

这里192.168.1.9是我的机器IP地址。在eclipse上调试时,我可以看到'request'中的值,但是在执行last HttpResponse response = client.execute(request);时它会出错。我对此很新,所以不确定我是否遗漏了一些东西。此外,我正在尝试检查rails服务器是否收到任何请求。什么都没有在那里发起。请指教。感谢。

2 个答案:

答案 0 :(得分:0)

在BasicNameValuePair中发送您的json对象 //这里是sear,shortvalue是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();
                }

答案 1 :(得分:0)

我认为问题出在URL中。 请尝试更改

http://192.168.1.9/3000/categories/create

为:

http://192.168.1.9:3000/categories/create

希望有所帮助,