在Android中使用cURL命令

时间:2014-07-05 22:01:48

标签: android androidhttpclient

我有以下cURL命令,我想从Android执行相同的操作,

curl -X POST -H "Content-Type: application/json" -d 

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

http://www.somesite.com/login

这就是我为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数组。我是cURL的新手,我不知道代码出了什么问题。

1 个答案:

答案 0 :(得分:2)

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

使用StringEntity代替UrlEncodedFormEntity。

构建一个包装KV字符串的JsonObject,然后使用'writer'将对象转换为httpclient中POST请求中StringEntity形式的字符串。

使用“Jackson”进行JsonObj实现和{​​{3}}的一些相关代码。

      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,当你需要验证时,它会为你提供HEADER / WIRE级别的日志记录你的机器人确实完全或几乎完全符合你的Curl客户端所做的事情。

下面是一个curl表达式的示例,后面是Android日志(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

当你习惯打开/关闭你的Android日志时,你在Curl进行连接测试的任何事情,你可以在android httpclient中实现它只要你放基本相同的标题,mimetype,post body (JsonAsString)在你的android中。