2013-08-06 47 views
4

我正尝试从我的Android应用程序向服务器发送JSON数据。数据库是MySQL,ROR用于服务器端代码。以下是用于发送数据的代码。无法将JSON数据从Android发送到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'中看到值,但是在执行最后一个HttpResponse response = client.execute(request);时发生错误。我对此很新,所以不确定我是否缺少一些东西。此外,我正试图检查轨道服务器是否收到任何请求。没有什么在那里开始的请指教。谢谢。

+1

什么错误它给你? –

+1

你给互联网许可吗? – Rohit

+0

发布完整的代码。你在ui线程上做网络相关的操作还是你使用asynctask? – Raghunandan

回答

0

我认为这个问题是在URL中。 。请尝试更改

http://192.168.1.9/3000/categories/create 

到:

http://192.168.1.9:3000/categories/create 

希望帮助,

+0

谢谢。这工作。 – Gaurav

0

发送您的JSON对象在BasicNameValuePair //这里瘢痕,是的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(); 
       } 
+0

谢谢。将按照建议尝试。 – Gaurav