Загрузить изображение :
Код: Выделить всё
public JsonObject uploadImage(Bitmap bitmap) throws IOException, ExecutionException, InterruptedException {
JsonObject JSONInput = new JsonObject();
JSONInput.addProperty(
"image", BASE_64_IMAGE_STRING
);
JsonObject response = new Requests().execute(JSONInput).get();
return response;
}
Код: Выделить всё
public class Requests extends AsyncTask {
@Override
protected JsonObject doInBackground(JsonObject... jsonObjects) {
URL url = null;
url = new URL("https://api.imgbb.com/1/upload?key=API_KEY&expiration=300");
HttpURLConnection connection = null;
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST")
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream()
OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
osw.write(jsonObjects[0].toString());
osw.flush();
osw.close();
connection.connect()
String result = new BufferedReader(new InputStreamReader(connection.getErrorStream()))
.lines().collect(Collectors.joining("\n"));
StringBuilder response = new StringBuilder();
BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jelem = gson.fromJson(response.toString(), JsonElement.class);
return jelem.getAsJsonObject();
}
@Override
protected void onPostExecute(JsonObject jsonObject) {
super.onPostExecute(jsonObject);
}
}
Однако я сохраняю получение {"status_code":400,"error":{"message":"Пустой источник загрузки".","code":130},"status_txt":"Плохо Запросите" обратно из API. Не могу понять, что делаю неправильно.
Я не могу найти ресурсы, использующие подобные API в Android. Приветствуется любое направление.
Я пытаюсь использовать класс HttpURLConnection для отправки API-запросов к ImageBB. Однако он, похоже, не распознает строку изображения в формате base64. прохождение
Подробнее здесь: https://stackoverflow.com/questions/793 ... in-android
Мобильная версия