我使用脚手架生成器设置了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”}'< / p>
{"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 app 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
我该如何解决这个问题?
答案 0 :(得分:0)
我从
替换了base_urlpublic static final String BASE_URL = "http://www.example.com/api/v1";
到
public static final String BASE_URL = "http://www.example.com/";
和来自
的界面public interface APIService {
@POST("/posts")
@FormUrlEncoded
Call<Imei> sendPost(@Field("title") String title);
}
到
public interface APIService {
@POST("/api/v1/posts")
@FormUrlEncoded
Call<Imei> sendPost(@Field("title") String title);
}