cURL在android java中使用httppost

时间:2013-01-27 19:12:44

标签: java android json curl libcurl

我正在尝试使用httpclient在android中获取cURL请求。

curl -k -X POST \
-F 'image=@00001_1.png;type=image/png' \
-F 'svgz=@00001_1.png;type=image/svg+xml' \
-F 'json={ 
    "text" : "Hello world!",
    "tid" : "0010",
    "timestamp" : "1342683312", 
    "location" : [ 22793, -553.3344],
    "facebook" :
    {
        "id": "4444444",
        "access_token": "7FUinHfxsCTrx",
        "expiration_date": "1358204400"                
    }
};type=application/json' \
https://example.com/api/posts

这是从SERVER给我一个BAD REQUEST ERROR(400)的代码。

public static void example() {
        HttpClient client = getNewHttpClient();
        HttpPost httpost = new HttpPost("https://example.com/api/posts");
        httpost.addHeader("image", "@00001_1.png; type=image/png");
        httpost.addHeader("svgz", "@00001_1.png; type=image/svg+xml");
        httpost.addHeader("type", "multipart/form-data");
//        httpost.setHeader("Content-type", "multipart/form-data");
        JSONObject data = new JSONObject();
        JSONObject facebook = new JSONObject();
        JSONArray location = new JSONArray();
        HttpResponse response = null;
        try {
            data.put("text","Hello world!");
            data.put("templateid","0010");
            data.put("timestamp","2012-07-08 09:00:45.312195368+00:00");

            location.put(37.7793);
            location.put(-122.4192);
            data.put("location", location);
            facebook.put("id", "4444444");
            facebook.put("access_token", "7FUinHfxsCTrx");
            facebook.put("expiration_date", "1358204400");
            data.put("facebook", facebook);

            System.out.println(" ---- data ----- "+data);

            StringEntity stringEntity = new StringEntity(data.toString(), "utf-8");
            httpost.setEntity(stringEntity);
            try {
                response = client.execute(httpost);
                System.out.println(" --- response --- "+response.getStatusLine().getStatusCode());
                HttpEntity entity = response.getEntity();
                // If the response does not enclose an entity, there is no need
                // to worry about connection release
                if(entity != null) {
                    // A Simple Response Read
                    InputStream instream = entity.getContent();
                    String result = convertStreamToString(instream);
                    System.out.println(" ---- result ---- "+result);

                    // Closing the input stream will trigger connection release
                    instream.close();
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

它从命令行运行得很完美,不管我哪里出错了。任何帮助高度赞赏。

如果我不使用任何用C语言编写的库(Libcurl等),我也可以告诉我。

感谢。

1 个答案:

答案 0 :(得分:0)

您已使用stringEntity发布数据。尝试使用UrlEncodedFormEntity发送数据。这里有一个例子:

try {
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("loginId", username.toString()));
    nvps.add(new BasicNameValuePair("password", password.toString()));
    nvps.add(new BasicNameValuePair("_eventId_submit", "Submit"));

    HttpPost httppost = new HttpPost("url2");
    httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    HttpParams params = httppost.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 45000);
    HttpConnectionParams.setSoTimeout(params, 45000);

    // Perform the HTTP POST request
    HttpResponse response = client.execute(httppost);
    status = response.getStatusLine().toString();
    if (!status.contains("OK")) {
        throw new HttpException(status);
    }

    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
            cookie = cookies.get(i);
        }
    }


} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (HttpException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}