【问题标题】:How to make a post request to a rails app using android and retrofit如何使用 android 和改造向 Rails 应用程序发出发布请求
【发布时间】:2017-11-14 09:54:25
【问题描述】:

我已经使用脚手架生成器设置了 rails api。所以我的控制器看起来像这样

Rails PostsController

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :update, :destroy]

  def index
    @posts = Post.all

    render json: @posts
  end

  def show
    render json: @post
  end

  def create
    @post = Post.new(post_params)

    if @post.save
      render json: @post, status: :created, location: @post
    else
      render json: @post.errors, status: :unprocessable_entity
    end
  end

  def update
    if @post.update(post_params)
      render json: @post
    else
      render json: @post.errors, status: :unprocessable_entity
    end
  end

  def destroy
    @post.destroy
  end

  private
    def set_post
      @post = Post.find(params[:id])
    end

    def post_params
      params.require(:post).permit(:title, :body, :userId)
    end
end

运行这个 curl 命令

curl -i -X POST -H "Content-Type:application/json" www.example.com/posts/ -d '{"title":"foo", "body":"bar"}'

{"id":4,"title":"foo","body":"bar","created_at":"2017-06-12T19:11:26.752Z","updated_at":"2017-06-12T19:11:26.752Z"}

然后我按照本教程 Sending Data With Retrofit 2 HTTP Client for Android 设置 android 应用程序

使用教程中的 link 运行应用程序有效。当我用我的 rails 应用程序 url 替换它时,我得到以下内容

06-12 21:44:01.352 31991-31991/? I/zygote: Not late-enabling -Xcheck:jni (already on)
06-12 21:44:01.365 31991-31991/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
06-12 21:44:01.403 31991-31999/? E/zygote: Failed writing handshake bytes (-1 of 14): Broken pipe
06-12 21:44:01.403 31991-31999/? I/zygote: Debugger is no longer active
06-12 21:44:01.494 31991-31991/? I/InstantRun: starting instant run server: is main process
06-12 21:44:01.600 31991-31991/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default
06-12 21:44:01.614 31991-32018/? D/OpenGLRenderer: HWUI GL Pipeline

                                                   [ 06-12 21:44:01.635 31991:32018 D/         ]
                                                   HostConnection::get() New Host Connection established 0xa95e4f00, tid 32018
06-12 21:44:01.637 31991-32018/? I/OpenGLRenderer: Initialized EGL, version 1.4
06-12 21:44:01.637 31991-32018/? D/OpenGLRenderer: Swap behavior 1
06-12 21:44:01.637 31991-32018/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
06-12 21:44:01.637 31991-32018/? D/OpenGLRenderer: Swap behavior 0
06-12 21:44:01.640 31991-32018/? D/EGL_emulation: eglCreateContext: 0xa718b220: maj 2 min 0 rcv 2
06-12 21:44:01.655 31991-32018/? D/EGL_emulation: eglMakeCurrent: 0xa718b220: ver 2 0 (tinfo 0xa95ccb10)
06-12 21:44:01.668 31991-32018/? W/android.hardware.graphics.mapper@2.0::Mapper: getService: found null hwbinder interface
06-12 21:44:01.672 31991-32018/? I/vndksupport: sphal namespace is not configured for this process. Loading /system/lib/hw/gralloc.ranchu.so from the current namespace instead.
06-12 21:44:01.707 31991-32018/? D/EGL_emulation: eglMakeCurrent: 0xa718b220: ver 2 0 (tinfo 0xa95ccb10)

并提交表格给我

06-12 21:46:52.742 31991-31997/com.kainet.retropost I/zygote: Do partial code cache collection, code=122KB, data=90KB
06-12 21:46:52.743 31991-31997/com.kainet.retropost I/zygote: After code cache collection, code=122KB, data=90KB
06-12 21:46:52.743 31991-31997/com.kainet.retropost I/zygote: Increasing code cache capacity to 512KB

我该如何解决这个问题?

【问题讨论】:

    标签: android ruby-on-rails api retrofit


    【解决方案1】:

    我替换了 base_url 来自

    public static final String BASE_URL = "http://www.example.com/api/v1";
    

    public static final String BASE_URL = "http://www.example.com/";
    

    和界面来自

    公共接口 APIService {

    @POST("/posts")
    @FormUrlEncoded
    Call<Imei> sendPost(@Field("title") String title);
    

    }

    公共接口 APIService {

    @POST("/api/v1/posts")
    @FormUrlEncoded
    Call<Imei> sendPost(@Field("title") String title);
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2021-05-10
      • 1970-01-01
      • 2019-06-21
      • 2011-08-05
      相关资源
      最近更新 更多