【发布时间】:2012-01-06 19:55:29
【问题描述】:
我目前正在开发一个通过 XML 和 JSON 与 Ruby on Rails 应用程序交互的 Android 应用程序。
我目前可以通过 XML 从我的网站中提取所有帖子,但我似乎无法通过 JSON 发布。
我的应用目前从一个看起来有点像这样的表单构建一个 JSON 对象:
{
"post": {
"other_param": "1",
"post_content": "Blah blah blah"
}
}
在我的服务器上,我相信我的帖子控制器中的 Create 方法设置正确: 定义创建 @post = current_user.posts.build(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
format.xml { render xml: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
format.xml { render xml: @post.errors, status: :unprocessable_entity }
end
end
end
在我的 android 应用程序中,我有一个方法将我之前发布的 JSON 对象作为参数以及用于进行身份验证的用户名和密码(身份验证工作我已经测试过了,是的,简单的 HTTP 身份验证可能不是最好的选择,但它是一个快速而肮脏的修复),然后它通过 HTTP POST 将 JSON 对象发送到 rails 服务器。就是这个方法:
public static void sendPost(JSONObject post, String email, String password)
{
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(new AuthScope(null,-1), new UsernamePasswordCredentials(email,password));
HttpPost httpPost = new HttpPost("http://mysite.com/posts");
JSONObject holder = new JSONObject();
try {
holder.put("post", post);
StringEntity se = new StringEntity(holder.toString());
Log.d("SendPostHTTP", holder.toString());
httpPost.setEntity(se);
httpPost.setHeader("Content-Type","application/json");
} catch (UnsupportedEncodingException e) {
Log.e("Error",""+e);
e.printStackTrace();
} catch (JSONException js) {
js.printStackTrace();
}
HttpResponse response = null;
try {
response = client.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.e("ClientProtocol",""+e);
} catch (IOException e) {
e.printStackTrace();
Log.e("IO",""+e);
}
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
entity.consumeContent();
} catch (IOException e) {
Log.e("IO E",""+e);
e.printStackTrace();
}
}
}
目前,当我调用此方法并将正确的 JSON 对象传递给它时,它什么也不做,我不知道为什么或如何找出问题所在。
我的 JSON 格式是否仍然错误,是否真的需要那个持有者围绕其他数据?还是我需要使用 HTTP POST 以外的东西?或者这只是 Rails 端的东西?不正确的路由或控制器?
如果有人能指出我正确的方向,我将不胜感激,因为我不知道从这里去哪里。
【问题讨论】:
-
代码看起来没问题。请求是否到达服务器?请求可以吗?服务器回复什么?还可以使用 EntityUtils 检索回复:developer.android.com/reference/org/apache/http/util/…
-
Android 的帖子存在一些问题,请尝试谷歌搜索,有解决方法。
-
def create @post = current_user.posts.build(params[:post]) 这是导致问题的部分。我的 android 应用程序没有将 current_user 传递给 rails,因此它显示为 nil 类。所以,我猜我必须从会话或其他东西中获取当前用户。我真的不知道。
-
@post = User.find_all_by_email(params[:useremail]).posts.build(params[:post]) 在帖子控制器中创建了另一个使用传递的电子邮件查找用户的方法,但现在设计认为它没有经过身份验证。控制台吐出:在 2011-11-28 18:22:06 -0800 开始 GET "/users/sign_in" for 127.0.0.1 由 Devise::SessionsController#new 作为 HTML 参数处理:{"session"=>{" action"=>"new", "controller"=>"devise/sessions"}} 渲染的 devise/shared/_links.erb (1.0ms) 在布局/应用程序中渲染的 devise/sessions/new.html.erb (60.5ms)在 605 毫秒内完成 200 次 OK
-
@Stealthnh 此代码完美运行,谢谢 :-)
标签: android ruby-on-rails-3 json rest http-post