4

我正在尝试将 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 上调试时,我可以看到“请求”中的值,但在执行 last 时会出错HttpResponse response = client.execute(request);。我对此很陌生,所以不确定我是否遗漏了什么。如果收到任何请求,我也正在尝试检查 rails 服务器。那里什么都没有启动。请指教。谢谢。

4

2 回答 2

0

将您的 json 对象发送到 BasicNameValuePair //这里是 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();
                }
于 2013-08-06T06:58:34.940 回答
0

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

http://192.168.1.9/3000/categories/create

到:

http://192.168.1.9:3000/categories/create

希望有帮助,

于 2013-08-06T07:00:42.753 回答