gpt4 book ai didi

android 如何使用 Retrofit 库发布 ByteArray?

转载 作者:行者123 更新时间:2023-11-30 01:42:11 28 4
gpt4 key购买 nike

public interface UploadImageService {

//send an image with params with Post
@Multipart
@POST("/image/upload_image.php")
void setUserImage(
@QueryMap Map<String, String> params,
@Part("pathImage") TypedFile file,
Callback<JsonElement> response);

我正在使用此代码发布 Bitmap 的 Bytearray,但无法获得成功。这是将相机捕获的图像发布到 php 服务器的最佳方式。

最佳答案

**Create an interface** 

public interface IMethodListener {

@Headers({
"Content-Type: application/json",
"User-Agent: bytearray"
})
@POST("/postByte")
public void postByte(@Body RequestLogin rObject, Callback<LoginModel> cb);

}

**Create Request and Response Model class**

public class RequestByteArray {
public String base64Array;
public RequestByteArray(String base64Array) {
this.base64Array = base64Array;
}
}

public class ByteArrayModel {
public boolean Status;
public String Message;
}

**Use below method in your activity or Fragment**

public void requestLoginApi(){
showSpinner(true);
RequestByteArray param = new RequestByteArray(byteArray.toString());
IMethodListener api = adapter.create(IMethodListener.class);
api.postByte(param, new Callback<LoginModel>() {
@Override
public void success(ByteArrayModel model, Response response) {
}

@Override
public void failure(RetrofitError error) {
}
});
}

**PHP function for converting base64 to row image file:**

function base64_to_jpeg($base64_string, $output_file) {
$ifp = fopen($output_file, "wb");

$data = explode(',', $base64_string);

fwrite($ifp, base64_decode($data[1]));
fclose($ifp);

return $output_file;
}

关于android 如何使用 Retrofit 库发布 ByteArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34340622/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com