2013-04-03 79 views
8

所以我想從我寫的Android應用程序發佈到Rails應用程序。我能夠從Rails應用程序內發佈成功。我還能夠使用稱爲簡單休息客戶端的Chrome添加成功發佈。從Android發佈到Ruby on Rails應用程序

enter image description here

當我嘗試從Android應用程式後擊中它的Rails應用程序,但創建一個空的職位。沒有任何輸入數據被軌道接收到。

我讀了第三方應用程序只能根據身份驗證從Rails應用程序獲取,以確保這不是我遇到的問題,我將其添加到了我的Rails配置中。

# de-activate tolken auth 
config.action_controller.allow_forgery_protection = false 

在這一點上我不能確定在何處我的問題在於,與我的Rails後端或我的Android客戶端。

好了,所以在我的控制器Rails的方法後,我試圖達到在這裏

# POST /orders 
    # POST /orders.json 
    def create 
    @order = Order.new(params[:order]) 

    respond_to do |format| 
     if @order.save 
     format.html { redirect_to @order, notice: 'Order was successfully created.' } 
     format.json { render json: @order, status: :created, location: @order } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @order.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

這裏是發送POST請求Android的Java代碼。 這是通過在用戶輸入數據的方法,我嘗試後

private void postInformationtoAPI() { 

       showToast("POSTING ORDER"); 
       List<NameValuePair> apiParams = new ArrayList<NameValuePair>(); 
       apiParams.add(new BasicNameValuePair("drinks_id", GlobalDrinkSelected)); 
       apiParams.add(new BasicNameValuePair("name", GlobalEditTextInputName)); 
       apiParams.add(new BasicNameValuePair("paid" , GlobalIsPaid)); 

       bgtPost = new BackGroundTaskPost(MAP_API_URL_POST_ORDER, "POST", apiParams); 
       bgtPost.execute(); 

       goToOrderCompleted(); 

      } 

這是它傳遞給,permorming的HTTP POST類。

public class BackGroundTaskPost extends AsyncTask<String, String, JSONObject> { 

    List<NameValuePair> postparams = new ArrayList<NameValuePair>(); 
    String URL = null; 
    String method = null; 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    public BackGroundTaskPost(String url, String method, List<NameValuePair> params) { 
     this.URL = url; 
     this.postparams = params; 
     this.method = method; 

     for (int i = 0; i < postparams.size(); i++){ 
      String test = postparams.get(i).toString(); 
      Log.d("This is in the lisht:", test); 
     } 
    } 

    @Override 
    protected JSONObject doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     // Making HTTP request 
     try { 
     // Making HTTP request 
     // check for request method 

     if (method.equals("POST")) { 
     // request method is POST 
     // defaultHttpClient 

      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(URL); 
      httpPost.setEntity(new UrlEncodedFormEntity(postparams, HTTP.UTF_8)); 
      Log.i("postparams : ", postparams.toString()); 
      httpPost.setHeader("Content-Type", "application/json"); 
      httpPost.setHeader("Accept", "application/json"); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     } else if (method == "GET") { 
     // request method is GET 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     String paramString = URLEncodedUtils 
      .format(postparams, "utf-8"); 
     URL += "?" + paramString; 
     HttpGet httpGet = new HttpGet(URL); 

     HttpResponse httpResponse = httpClient.execute(httpGet); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 
     } 

     } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 

     try { 
      Log.i("Logging out *is* before beffered reader", is.toString()); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
     is, "utf-8"), 8); 
     Log.i("Logging out *is* after beffered reader", is.toString()); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
     sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
     Log.i("json: ",json); 
     } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
     jObj = new JSONObject(json); 
     } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data TEST " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 
    } 

這是什麼日誌的在上面的類postparams出來,所以我知道數據實際上被髮送

04-03 21:36:23.994: I/postparams :(690): [drinks_id=41, name=Dave, paid=True] 

這就是登錄貓是表示從服務器的響應

04-03 20:56:08.247: I/json:(690): {"created_at":"2013-04-03T20:56:06Z","drinks_id":null,"id":1351,"name":null,"paid":null,"served":null,"updated_at":"2013-04-03T20:56:06Z"} 

我真的很努力去理解問題出在哪裏,並且已經停留了很長時間。任何有識之士將不勝感激。如果需要更多的信息,只需留言。

編輯:從服務器

日誌,這是從簡單的REST客戶端成功後

2013-04-03T23:13:31+00:00 app[web.1]: Completed 200 OK in 15ms (Views: 8.7ms | ActiveRecord: 5.2ms) 
2013-04-03T23:13:42+00:00 app[web.1]: Started POST "/orders.json" for 89.101.112.167 at 2013-04-03 23:13:42 +0000 
2013-04-03T23:13:42+00:00 app[web.1]: Processing by OrdersController#create as JSON 
2013-04-03T23:13:42+00:00 app[web.1]: Parameters: {"updated_at"=>nil, "drinks_id"=>51, "id"=>1021, "name"=>"Test", "paid"=>true, "served"=>nil, "created_at"=>nil, "order"=>{"drinks_id"=>51, "name"=>"Test", "paid"=>true, "served"=>nil}} 
2013-04-03T23:13:43+00:00 heroku[router]: at=info method=POST path=/orders.json host=fyp-coffeeshop.herokuapp.com fwd="89.101.112.167" dyno=web.1 connect=1ms service=25ms status=201 bytes=138 
2013-04-03T23:13:43+00:00 app[web.1]: Completed 201 Created in 15ms (Views: 0.6ms | ActiveRecord: 13.2ms) 

這是從Android應用程序發佈

2013-04-03T22:56:45+00:00 app[web.1]: Started POST "/orders.json" for 89.101.112.167 at 2013-04-03 22:56:45 +0000 
2013-04-03T22:56:45+00:00 app[web.1]: Processing by OrdersController#create as JSON 
2013-04-03T22:56:45+00:00 app[web.1]: Completed 201 Created in 23ms (Views: 2.2ms | ActiveRecord: 16.3ms) 
2013-04-03T22:56:45+00:00 heroku[router]: at=info method=POST path=/orders.json host=fyp-coffeeshop.herokuapp.com fwd="89.101.112.167" dyno=web.1 connect=4ms service=37ms status=201 bytes=138 
+0

你可以寫一個快速的PHP腳本來轉儲參數列表中後,以確保它到達那裏?將$ _POST數組扔到一個文件中並檢查它。 – Dave 2013-04-03 22:20:09

+0

我只是想着更多。我從來沒有發過一個列表,並說它是JSON內容,我一直創建JSONObject,然後發佈名稱/值對query = jsonobject,然後在我的服務器上將查詢值作爲json字符串。 – Dave 2013-04-03 22:27:40

+0

我只是從剩餘的客戶端和我的應用程序登出了一個請求比較,我會更新我的問題 – DavedCusack 2013-04-03 22:58:46

回答

6

你設置的內容類型的JSON,但不實際發送JSON,你正在發送標準的POST URL編碼參數。

實際上你需要發送一個JSON對象:

JSONObject params = new JSONObject(); 
params.put("drinks_id", GlobalDrinkSelected); 
params.put("name", GlobalEditTextInputName); 
params.put("paid", GlobalIsPaid); 

StringEntity entity = new StringEntity(params.toString()); 
httpPost.setEntity(entity); 
+0

我的問題中的代碼是否未添加參數postInformationtoAPI()方法? 編輯:謝謝你的幫助 – DavedCusack 2013-04-04 00:15:22

+1

科迪非常感謝你爲我節省了很多心痛。通過傳入JSON對象並將其添加到實體「entity.setContentType(」application/json; charset = UTF-8「);」我能夠發佈到服務器 – DavedCusack 2013-04-04 00:50:05

1

的問題是,當你'在Android中構建POST時,你會覆蓋實體(body)。您最初設置它,然後再次設置它,有效清除您已設置的內容。

這是正確的:

httpPost.setEntity(new UrlEncodedFormEntity(postparams)); 

但隨後幾行後,你就帶過寫:

httpPost.setEntity(new StringEntity("UTF-8")); 

所以溝是第二setEntity()電話。

你可以達到你想要做什麼 - 通過調整喜歡你的代碼中設置了POST身體UTF-8:

httpPost.setEntity(new UrlEncodedFormEntity(postparams, HTTP.UTF_8)); 
+0

感謝您的答覆科迪,我明白你的意思,並嘗試現在實施它 – DavedCusack 2013-04-03 23:37:19

+0

雖然im確定糾正實體沒有幫助,參數仍然沒有達到服務器。我也必須做不正確的事情。我將更新問題中的代碼,以反映我不在使用的內容 – DavedCusack 2013-04-03 23:46:13

+0

*我將更新問題中的代碼以反映我目前使用的代碼 – DavedCusack 2013-04-03 23:52:42