Android 获取java.io.EOFException:使用改型2将映像上载到服务器时,第1行第1列的输入结束
我正在尝试使用改进2多部分请求和Gson解析器将一个映像上载到服务器 我用postman测试了api,它工作得很好,但在android中我得到了提到的异常 这是我的密码 这是改装依赖Android 获取java.io.EOFException:使用改型2将映像上载到服务器时,第1行第1列的输入结束,android,retrofit2,multipartform-data,Android,Retrofit2,Multipartform Data,我正在尝试使用改进2多部分请求和Gson解析器将一个映像上载到服务器 我用postman测试了api,它工作得很好,但在android中我得到了提到的异常 这是我的密码 这是改装依赖 implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.retrofit2:converter-gson:2.5.0' Api客户端 public class ApiClient { public s
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
Api客户端
public class ApiClient {
public static final String BASE_URL = "http://www.AAbd.com/api/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
Gson gson = new GsonBuilder()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
Api接口
@POST("edit/Image")
@Multipart
Call<AuthModel> addImage (@Part("api_token") RequestBody api_token,
@Part("image") MultipartBody.Part image);
调用请求
public void addImageRequest (Bitmap image) {
try {
File f = new File(getCacheDir(), "offf");
f.createNewFile();
Bitmap bitmap = image;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), f);
MultipartBody.Part body = MultipartBody.Part.createFormData("image", f.getName(), reqFile);
RequestBody tokenRequest = RequestBody.create(MediaType.parse("text/plain"), token2);
apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<AuthModel> call = apiInterface.addImage(tokenRequest, body);
call.enqueue(new Callback<AuthModel>() {
@Override
public void onResponse(Call<AuthModel> call, Response<AuthModel> response) {
AuthModel result = response.body();
if(result != null) {
String status = result.getStatus();
String msg = result.getMessage();
if(status.equals("true")) {
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
Log.e("errorIs", msg);
} else {
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
Log.e("errorIs", msg);
}
} else {
try {
//Toast.makeText(context, response.errorBody().string() , Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<AuthModel> call, Throwable t) {
Toast.makeText(MainActivity.this, t.toString(), Toast.LENGTH_LONG).show();
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void addImageRequest(位图图像){
试一试{
文件f=新文件(getCacheDir(),“offf”);
f、 createNewFile();
位图=图像;
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
compress(bitmap.CompressFormat.JPEG,0/*对于PNG*/,bos被忽略);
字节[]位图数据=bos.toByteArray();
FileOutputStream fos=新的FileOutputStream(f);
fos.写入(位图数据);
fos.flush();
fos.close();
RequestBody reqFile=RequestBody.create(MediaType.parse(“image/*”),f);
MultipartBody.Part body=MultipartBody.Part.createFormData(“image”,f.getName(),reqFile);
RequestBody-tokenRequest=RequestBody.create(MediaType.parse(“text/plain”),token2);
apinterface=ApiClient.getClient().create(apinterface.class);
Call Call=apinterface.addImage(令牌请求,主体);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
AuthModel result=response.body();
如果(结果!=null){
字符串状态=result.getStatus();
字符串msg=result.getMessage();
if(status.equals(“true”)){
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG.show();
Log.e(“errorIs”,msg);
}否则{
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG.show();
Log.e(“errorIs”,msg);
}
}否则{
试一试{
//Toast.makeText(上下文,response.errorBody().string(),Toast.LENGTH_LONG.show();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(MainActivity.this,t.toString(),Toast.LENGTH_LONG).show();
}
});
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
我检查了之前关于这个问题的所有问题,但没有找到一个可能的解决方案。请提前帮助和感谢您是否至少能够获取图像和文件?@Skizo ozᴉʞ是的,这是肯定的。您的模型包含设置和获取还是仅获取?@Skizo ozᴉʞS get onlyFirewall配置有点麻烦。我建议您在安全性较低的情况下测试应用程序。关闭防火墙。此外,在大数据中的http请求中,您应该添加标头保持活动连接。您至少能够获取映像并获取其文件吗?@Skizo ozᴉʞ是的,这是肯定的。您的模型包含设置和获取还是仅获取?@Skizo ozᴉʞS get onlyFirewall配置有点麻烦。我建议您在低安全性下测试应用程序。关闭防火墙。此外,在大数据中的http请求中,您应该添加标头保持活动连接。
public void addImageRequest (Bitmap image) {
try {
File f = new File(getCacheDir(), "offf");
f.createNewFile();
Bitmap bitmap = image;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), f);
MultipartBody.Part body = MultipartBody.Part.createFormData("image", f.getName(), reqFile);
RequestBody tokenRequest = RequestBody.create(MediaType.parse("text/plain"), token2);
apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<AuthModel> call = apiInterface.addImage(tokenRequest, body);
call.enqueue(new Callback<AuthModel>() {
@Override
public void onResponse(Call<AuthModel> call, Response<AuthModel> response) {
AuthModel result = response.body();
if(result != null) {
String status = result.getStatus();
String msg = result.getMessage();
if(status.equals("true")) {
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
Log.e("errorIs", msg);
} else {
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
Log.e("errorIs", msg);
}
} else {
try {
//Toast.makeText(context, response.errorBody().string() , Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<AuthModel> call, Throwable t) {
Toast.makeText(MainActivity.this, t.toString(), Toast.LENGTH_LONG).show();
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}