私はPOSTを使用してリクエストする必要がある別のApiサービスに直面していますが、ボディコンテンツがない場合、イメージをbase64に変換して送信していますその問題と私はそれが動作しませんでした、この "ソリューション" を見つけ:OKHttpを使用して空のボディでPOSTを行う方法
1:
RequestBody reqbody = RequestBody.create(null, new byte[0]);
Request.Builder formBody = new Request.Builder().url(url).method("POST",reqbody).header("Content-Length", "0");
2:
request = new Request.Builder()
.url(BASE_URL + route)
.method("POST", RequestBody.create(null, new byte[0]))
.post(requestBody)
.build();
でもI POSTメソッドで明示的に言って、それGETリクエストを送信し続けます。 POST要求。ありがとう!
My活動:
public String SendImage(String image64) throws IOException{
//RequestBody reqbody = RequestBody.create(null, new byte[0]);
Request request = new Request.Builder()
.url("http://ap.imagensbrasil.org/api/1/upload/?key=9c9dfe77cd3bdbaa7220c6bbaf7452e7&source=" + image64 + "&format=txt")
.method("POST", RequestBody.create(null, new byte[0]))
.header("Content-Length", "0")
.build();
OkHttpClient Client = client.newBuilder() .readTimeout(25, TimeUnit.SECONDS).build();
Response response = Client.newCall(request).execute();
return response.body().string();
}
しかし、ここで私は、URLを入れましたか?そのAPIには送信するコンテンツがありません。私はBody Stringも持っていません。すべてがURLの中に入ります。私の更新を見てください –
このコードを試してください。 – Rohit
メソッド.post( "")は文字列を受け入れないので、私はしました:RequestBody body = RequestBody.create(JSON、 ""); .post(body)とします。 –