android6 curl移植,androidhttpclient

Use JSONObject instead of just a List of name/value pairs.

Use a StringEntity instead of a UrlEncodedFormEntity.

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.

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));

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.

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值