在 Android 中使用 cURL 命令

Using cURL commands in Android

提问人:Falling Into Infinity 提问时间:7/6/2014 最后编辑:Falling Into Infinity 更新时间:7/8/2015 访问量:8690

问:

我有以下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的新手,我不知道代码出了什么问题。

AndroidHttpClient

评论

0赞 CommonsWare 7/6/2014
stackoverflow.com/questions/2323617/ 的副本...

答:

2赞 Robert Rowntree 7/6/2014 #1

使用 JSONObject,而不仅仅是名称/值对的列表。

使用 StringEntity 而不是 UrlEncodedFormEntity。

构建一个包装 KV 字符串的 JsonObject,然后使用“writer”将 Object 转储到 httpclient 中 POST 请求上 StringEntity 形式的字符串。

一些使用“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 进行测试,然后在 android 中完全重新实现 curl 是一种非常好的技术,只要您能够在 android httpclient 中打开 LOGGER,当您需要验证您的 android 是否完全或几乎完全准确地执行您的 Curl 客户端正在做什么时,它会为您提供 HEADER / WIRE 级别的日志记录。

以下示例是 curl 表达式,后跟 Android 日志 (WIRE/HEADERS),显示了您使用 Curl 发送的相同内容的 Android 类似物。

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 日志时,您在 Curl 中为连接测试所做的任何事情,您都可以在 android httpclient 中实现,只要您在 android 中放置基本相同的标头、mimetype、帖子正文 (JsonAsString),它就会起作用。