繁体   English   中英

带参数的Android HTTP发布文件

[英]Android HTTP Post File with Parameters

我需要使用以下规则发布文件:

$ curl -X POST https://www.strava.com/api/v3/uploads \
-H "Authorization: Bearer 83ebeabdec09f6670863766f792ead24d61fe3f9" \
-F activity_type=ride \
-F file=@temp.gpx \
-F data_type=gpx

我有授权码,我有文件。 我已经看到了很多有关堆栈溢出的示例,但是,我不知道如何将它们全部在一起,也不知道应将“ -F”和“ -H”作为参数传递。

这是解决方案,我发现“ -H”代表“ header”,并且有它自己的位置:

 HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://www.strava.com/api/v3/uploads");

        try {
            MultipartEntity entity = new MultipartEntity();
            FileBody bin = new FileBody(new File(FilePath));


            httppost.setHeader("Authorization:", "Bearer " + Token);

            entity.addPart("activity_type", new StringBody("run"));
            entity.addPart("file", bin);
            entity.addPart("data_type", new StringBody("GPX"));

            httppost.setEntity(entity);



            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httppost, responseHandler);
            Log.i("Kenyan Runner", "Response of file upload: " + responseBody);

        } catch (ClientProtocolException e) {
            Log.e("Kenyan Runner", e.toString());
        } catch (IOException e) {
            Log.e("Kenyan Runner", e.toString());
        }finally {
            httpclient.getConnectionManager().shutdown();
        }

暂无
暂无

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

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