在Android中使用cURL命令

4
我有以下cURL命令,我想从Android上执行相同的操作,
curl -X POST -H "Content-Type: application/json" -d 

'{"username":"user","password":"pass"}' 

http://www.somesite.com/login

这是我为安卓设备制作的内容:

    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数组。 我对cURL很新,不知道代码出了什么问题。

Android HttpPost:如何获取结果 - CommonsWare
1个回答

2
使用JSONObject代替仅使用名称/值对的列表。
使用StringEntity代替UrlEncodedFormEntity。
构建一个JsonObject包装您的KV字符串,然后使用“writer”将对象转储为字符串形式的StringEntity,通过httpclient进行POST请求。
一些相关代码使用“Jackson”进行JsonObj实现和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));

首先使用curl -VERBOSE进行测试,然后在安卓中精确地重新实现curl是一种非常好的技术,只要你能在安卓httpclient中打开LOGGER,这样当你需要验证你的安卓是否与Curl客户端做了完全相同或几乎相同的事情时,就可以获得HEADER/WIRE级别的日志记录。

下面是一个curl表达式示例,后面是安卓日志(WIRE/HEADERS),显示了使用安卓发送与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

当你习惯于在安卓设备上开启/关闭日志记录时,你所进行的任何Curl连接测试操作都可以直接应用在安卓httpclient中。只要你在安卓设备中设置基本相同的头部信息、媒体类型和请求体(JsonAsString),它就能正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接