问题描述
我有以下的卷曲的命令,我想从Android的执行同样的事情,
I have the following cURL command and I would like to execute the same thing from Android,
curl -X POST -H "Content-Type: application/json" -d
'{"username":"user","password":"pass"}'
http://www.somesite.com/login
这是我编的Andorid的,
This is what I made for Andorid,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.somesite.com/login");
try {
httppost.addHeader("Content-Type", "application/json");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "user"));
nameValuePairs.add(new BasicNameValuePair("password", "pass"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
Log.d("RESPOND", EntityUtils.toString(response.getEntity()));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
当我运行的应用程序,我得到了整个页面的响应,而不是期望的JSON数组。我是新的卷曲,我不知道什么是与code去错了。
When I ran the application I get the whole page as a response instead of the desired JSON array. I'm new in cURL and I have no idea what's going wrong with the code.
推荐答案
使用的JSONObject 而不是名称/值对的只是一个列表。
Use JSONObject instead of just a List of name/value pairs.
使用一个 StringEntity的而不是UrlEn codedFormEntity。
Use a StringEntity instead of a UrlEncodedFormEntity.
建立一个JSONObject的包裹你的KV的字符串,然后用'作家'的对象转储到一个字符串在HttpClient的POST请求一个StringEntity的形式。
Build a JsonObject wrapping your KV strings and then use a 'writer' to dump the Object to a string in the form of a StringEntity on the POST request in httpclient.
用禅师的JsonObj执行一些相关的code和 httpclientandroid 一>。
some relevant code using "Jackson" for the JsonObj implementation and httpclientandroid.
ObjectNode rootOb = new ObjectMapper().createObjectNode();
rootOb.put("username",user );
...
StringWriter writer = new StringWriter();
try {
new ObjectMapper().writeValue(writer, rootOb);
} catch (JsonGenerationException e) {
}
String poststr=writer.toString();
new HttpConnection(handler4).post(url, poststr);
...
httpPost.setEntity(new StringEntity(poststr));
测试与卷曲-verbose第一然后就重新实现完全在android的卷曲是一个很好的方法,只要你能打开记录器在Android的HttpClient,让你页眉/线级别的记录,当你需要验证你的android做精确或几乎完全你的卷发用户的操作。
testing with curl -VERBOSE first then just reimplement exactly the curl in android is a very good technique as long as you are able to turn on LOGGER in the android httpclient that gives you HEADER / WIRE level logging when you need to verify that your android does EXACT or almost exact what your Curl client was doing.
例子的卷曲EX pression其次是Android的日志(WIRE /头)显示了您发送使用curl同样的东西Android的类似物如下。
example below of a curl expression followed by Android logs (WIRE/HEADERS) showing android analogs of the same stuff you sent using Curl.
curl -v -X POST \
-H "X-Parse-Application-Id: LAbR" \
-H "X-Parse-REST-API-Key: ke" \
-H "Content-Type: application/json" \
-d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \
https://api.parse.com/1/classes/GameScore
D/ch.boye.httpclientandroidlib.wire(18636): >> "POST /1/files/audio HTTP/1.1[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "X-Parse-Application-Id: LAbR[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "X-Parse-REST-API-Key: kuI9[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "Content-Type: audio/*[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "Content-Length: 12074[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "Host: api.parse.com[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "Connection: Keep-Alive[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "--"
D/ch.boye.httpclientandroidlib.wire(18636): >> "cVxX6b-jxQnxFCczaKHLNZ_Hq8HI9AEW219GW3w"
D/ch.boye.httpclientandroidlib.wire(18636): >> "[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "Content-Disposition"
D/ch.boye.httpclientandroidlib.wire(18636): >> ": "
D/ch.boye.httpclientandroidlib.wire(18636): >> "form-data; name="bin"; filename="myfile.3gp""
D/ch.boye.httpclientandroidlib.wire(18636): >> "[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "Content-Type"
D/ch.boye.httpclientandroidlib.wire(18636): >> ": "
D/ch.boye.httpclientandroidlib.wire(18636): >> "application/octet-stream"
D/ch.boye.httpclientandroidlib.wire(18636): >> "[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "[\r][\n]"
D/ch.boye.httpclientandroidlib.headers(18636): >> POST /1/files/audio HTTP/1.1
D/ch.boye.httpclientandroidlib.headers(18636): >> X-Parse-Application-Id: LAbR
D/ch.boye.httpclientandroidlib.headers(18636): >> X-Parse-REST-API-Key: kuI9
D/ch.boye.httpclientandroidlib.headers(18636): >> Content-Type: audio/*
D/ch.boye.httpclientandroidlib.headers(18636): >> Content-Length: 12074
D/ch.boye.httpclientandroidlib.headers(18636): >> Host: api.parse.com
D/ch.boye.httpclientandroidlib.headers(18636): >> Connection: Keep-Alive
当你习惯了开/关你的android日志,任何你在卷曲做连接测试开启,则只能实现机器人的HttpClient只要你把基本相同的头文件,将工作,MIMETYPE,帖子正文(JsonAsString)在你的Android。
When you get used to turning on/off your android logs, anything you do in Curl for connection tests, you can then just implement in android httpclient and it will work as long as you put basically same headers, mimetype, post body (JsonAsString) in your android.
这篇关于使用Android中卷曲的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!