【问题标题】:Http Post on Android with Header and File带有标题和文件的Android上的Http Post
【发布时间】:2015-08-28 09:17:51
【问题描述】:

我必须在 Android 中复制 thia curl 命令:

curl -X POST -F gps_track=@ANTON_MORODER.igc https://myserver -H "api-name:name" -H "api-key:key" -H "user-key:user" -H "Content-类型:应用程序/json"

我绑定了几种方法,但总是从服务器获得 400:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(doarama_api_url+"/activity");
    httppost.setHeader("api-name",api_name);
    httppost.setHeader("api-key",api_key);
    httppost.setHeader("user-key", AppSettings.getdoaramatUserKey() );
    httppost.setHeader("Accept", "application/json");
    InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(inIGC), -1);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(true); // Send in multiple parts if needed
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    Log.d("DOARAMA", "statusCode: " + String.valueOf(statusCode));

谢谢

【问题讨论】:

  • 您是否在清单中添加了互联网权限?
  • 那是什么意思,400?你从哪里得到的?
  • I have to replicate thia curl comand。你忘了解释这个 curl 命令在做什么。该命令中的“文件”在哪里?
  • 对不起..文件在:文件 gpx_or_igcFolder = new File(Environment.getExternalStorageDirectory(), "TTLiveTrack24"); String inIGC = gpx_or_igcFolder.getPath()+ "/" + 文件名 + ".igc";
  • 400 ???这是什么意思!!!!我问那个 CURL 命令中的文件在哪里。并解释你的 curl 命令。你是认真的吗?

标签: android curl http-post


【解决方案1】:

替换: 接受 -> 内容类型

【讨论】:

  • 虽然这可能会回答 OP 的问题,但几句话的解释会让当前和未来的读者更容易理解。
猜你喜欢
  • 2018-09-04
  • 2016-03-12
  • 2017-03-23
  • 2015-10-06
  • 2019-10-16
  • 1970-01-01
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多