简体   繁体   English

从android发布到Ruby on Rails应用程序

[英]Posting to Ruby on Rails app from android

So I am trying to post to a rails app from an Android app I am writing. 所以我想从我正在写的Android应用程序发布到rails应用程序。 I am able to post successful from inside the rails app. 我能够从rails app中发布成功。 I was also able to post successfully using a chrome add on called Simple Rest client. 我还能够使用名为Simple Rest客户端的chrome add成功发布。

在此输入图像描述

When I try and post from the Android app its hitting the rails app but creating an empty post. 当我尝试从Android应用程序发布时,它会点击rails应用程序,但会创建一个空帖子。 None of the input data is being received by rails. rails没有接收任何输入数据。

I read that 3rd party applications are only able to GET from a Rails app depending on authentication so to make sure this wasn't the issue I was having I added this to my Rails config. 我读到第三方应用程序只能从Rails应用程序获取,具体取决于身份验证,以确保这不是我遇到的问题我将此添加到我的Rails配置。

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

At this point I am unsure as to where my issue lies, with my Rails backend or my Android client. 在这一点上,我不确定我的问题在哪里,我的Rails后端或我的Android客户端。

ok so the Rails post method in my controller that I'm trying to reach is here 好的,所以我想要触及的控制器中的Rails post方法就在这里

# 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

Here is the Android java code for sending the Post Request. 这是用于发送Post Request的Android java代码。 This is the method passing in the User input data I am trying to POST 这是传递我尝试POST的用户输入数据的方法

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

            }

And this is the class that it is passed to, permorming the HTTP POST. 这是传递给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;

     }
    }

This is the what log's out for postparams in the above class, so I know data is actually being sent 这是上述类中postparams的日志,所以我知道实际上正在发送数据

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

This is what Log Cat is showing as a response from the server 这就是Log Cat作为服务器响应显示的内容

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"}

I am really struggling to understand where the issue lies with this and have been stuck on it for quite awhile. 我真的很难理解这个问题在哪里,并且已经坚持了很长一段时间。 Any insight would be much appreciated. 任何见解都会非常感激。 And if any more information is needed just shout. 如果需要更多信息,请大声喊叫。

Edit: logs from server 编辑:来自服务器的日志

This is a successful post from the simple REST client 这是一个来自简单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)

This is from the android app posting 这是来自Android app发布

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

You're setting a content-type of JSON but not actually sending JSON, you're sending standard POST url-encoded parameters. 您正在设置JSON的内容类型,但实际上并未发送JSON,您正在发送标准的POST网址编码参数。

You need to actually send a JSON object: 您需要实际发送一个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);

The problem is that when you're building the POST in Android you're over-writing the entity (the body). 问题是,当您在Android中构建POST时,您将覆盖实体(正文)。 You initially set it and then you set it again, effectively clearing out what you already set. 您最初设置它然后再次设置它,有效地清除您已设置的内容。

This is correct: 这是对的:

httpPost.setEntity(new UrlEncodedFormEntity(postparams));

But then a couple of lines later you over-write it with: 但是之后几行你用以下内容覆盖它:

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

So ditch that 2nd setEntity() call. 所以第二个setEntity()调用沟。

You can achieve what you're trying to do - setting the POST body in UTF-8 by tweaking your code like: 你可以实现你想要做的 - 通过调整你的代码来设置UTF-8的POST主体:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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