简体   繁体   English

cURL使用android java中的httppost

[英]cURL using httppost in android java

I'm trying to get a cURL request work in android using the httpclient. 我正在尝试使用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

This is the code that's giving me a BAD REQUEST ERROR (400) from SERVER. 这是从服务器发出错误请求错误(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();
        }
    }

It works perfect from the command line, don't where I'm going wrong. 从命令行可以完美运行,别误会。 Any help highly appreciated. 任何帮助,高度赞赏。

Also let me know if I can do it without using any library written in C (Libcurl, etc). 还请告诉我是否可以在不使用任何用C编写的库(Libcurl等)的情况下做到这一点。

Thanks. 谢谢。

You have used stringEntity to post data. 您已使用stringEntity发布数据。 Try using UrlEncodedFormEntity to send data. 尝试使用UrlEncodedFormEntity发送数据。 Here one example how to do: 这里有一个例子怎么做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM