簡體   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