如何使用android和改型向rails应用程序发出post请求
我已经使用scaffold生成器设置了rails api。我的控制器看起来像这样 Rails PostsController如何使用android和改型向rails应用程序发出post请求,android,ruby-on-rails,api,retrofit,Android,Ruby On Rails,Api,Retrofit,我已经使用scaffold生成器设置了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
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
然后,我按照本教程设置android应用程序
在教程中使用运行应用程序是有效的
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
如何修复此问题?我替换了
public static final String BASE_URL = "http://www.example.com/api/v1";
到
以及来自的接口
公共接口服务{
@POST("/posts")
@FormUrlEncoded
Call<Imei> sendPost(@Field("title") String title);
@POST("/api/v1/posts")
@FormUrlEncoded
Call<Imei> sendPost(@Field("title") String title);
@POST(“/posts”)
@FormUrlEncoded
调用sendPost(@Field(“title”)字符串title);
}
到
公共接口服务{
@POST("/posts")
@FormUrlEncoded
Call<Imei> sendPost(@Field("title") String title);
@POST("/api/v1/posts")
@FormUrlEncoded
Call<Imei> sendPost(@Field("title") String title);
@POST(“/api/v1/posts”)
@FormUrlEncoded
调用sendPost(@Field(“title”)字符串title);
}