问题描述
我有一个Android应用程序发布到我的西纳特拉服务。早些时候,我无法对我的西纳特拉服务中读取参数。然而,当我设置内容类型为X-WWW的形式urlen codeD。我能看到PARAMS,但倒不怎么我想要的。我得到的东西这是我的要求PARAMS我西纳特拉的服务。
I have an android app posting to my sinatra service. Earlier I was not able to read parameters on my sinatra service. However, after I set content type to "x-www-form-urlencoded". I was able to see params but not quite how I wanted. I am getting something this as my params of the request at my sinatra service.
{"{\"user\":{\"gender\":\"female\"},\"session_id\":\"7a13fd20-9ad9-45c2-b308-
8f390b4747f8\"}"=> nil, "splat"=>["update_profile.json"], "captures"=>["update_profile.json"]}
这是我是如何从我的应用要求。
This is how I am making request from my app.
StringEntity se;
se = new StringEntity(getJsonObjectfromNameValueList(params.get_params(), "user");
se.setContentType("application/x-www-form-urlencoded");
postRequest.setEntity(se);
private JSONObject getJsonObjectfromNameValueList(ArrayList<NameValuePair> _params, String RootName) {
JSONObject rootjsonObject = new JSONObject();
JSONObject jsonObject = new JSONObject();
if (_params != null) {
if (!_params.isEmpty()) {
for (NameValuePair p : _params) {
try {
if (p.getName().equals(ApplicationFacade.SESSION_ID))
rootjsonObject.put((String) p.getName(), (String) p.getValue());
else
jsonObject.put((String) p.getName(), (String) p.getValue());
} catch (JSONException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
}
try {
rootjsonObject.put(RootName, jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
return rootjsonObject;
}
我试着用用给出的方法:
这是我如何使用在链接中提到的方法做了我的请求。
This is how I made my request using the method mentioned in the link.
public ArrayList<NameValuePair> getJsonObjectfromNameValueList(ArrayList<NameValuePair> _params, String RootName){
ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
JSONObject jsonObject = new JSONObject();
if (_params != null) {
if (!_params.isEmpty()) {
for (NameValuePair p : _params) {
try {
if (p.getName().equals(ApplicationFacade.SESSION_ID))
arrayList.add(new BasicNameValuePair((String) p.getName(), (String) p.getValue()));
else
jsonObject.put((String) p.getName(), (String) p.getValue());
} catch (JSONException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
}
arrayList.add(new BasicNameValuePair(RootName, jsonObject.toString()));
return arrayList;
}
我做出上述变化是在此之后得到的回应:
The response I got after making the above change was this:
{"session_id"=>"958c7dee-f12c-49ec-ab0c-932e9a4ed173",
"user"=>"[gender=male]",
"splat"=>["update_profile.json"],
"captures"=>["update_profile.json"]}
pretty接近,但性别=男是不可取的。我需要通过这些参数来盲目的另一项服务,所以我需要让他们的权利。
Pretty close but that "gender=male" is not desirable. I need to pass these arguments to another service blindly, so I need to get them right.
这是我想在我的西纳特拉服务PARAMS跟随。
The params that I want at my sinatra service are following.
{"session_id" : "958c7dee-f12c-49ec-ab0c-932e9a4ed173",
"user":
{
"gender" : "male"
}
}
推荐答案
原来,问题出西纳特拉获取其PARAMS的方式。
我是能够解决使用凯尔的这个帖子真棒回应我的问题:Sinatra控制器PARAMS法空JSON的POST请求来
It turns out that the problem is with the way Sinatra gets its params.I was able to solve my problem using Kyle's awesome response to this post: Sinatra controller params method coming in empty on JSON post request
code:
before do
if request.request_method == "POST" and request.content_type=="application/json"
body_parameters = request.body.read
parsed = body_parameters && body_parameters.length >= 2 ? JSON.parse(body_parameters) : nil
params.merge!(parsed)
end
end
备用方法:
然而,我发现了一个更好的解决办法来对付它,通过添加一个中间件为我做相同的。我用机架的contrib
宝石。
以下是我在code做了更改:
Alternate Method:However, I found a better solution to deal with it, by adding a middleware to do the same for me. I used rack-contrib
gem.Following are the changes I did in my code:
修改
使用git获取哪里的问题涉及到的内容类型应用程序/ JSON的版本;字符集= UTF-8
固定
use git to get the version where issue related to content type application/json;charset=UTF-8
is fixed
的Gemfile:
gem 'rack-contrib', git: '[email protected]:rack/rack-contrib', ref: 'b7237381e412852435d87100a37add67b2cfbb63'
config.ru
use Rack::PostBodyContentTypeParser
来源:
这篇关于从Java到西纳特拉服务发布JSON PARAMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!