我有上傳服務器上使用翻新圖片的問題2.可以幫我嗎?我做錯了什麼?我的後端開發具有傷心我在頭中邊界didnt產生=( 這是我的接口:用retrofit2上傳照片:翻新:2.0.0-beta4
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.Call;
import retrofit2.http.Multipart;
import okhttp3.MultipartBody;
import com.google.gson.JsonElement;
public interface Retrofitv2Api {
@Multipart
@Headers("Content-Type: multipart/form-data;")
@POST("/mobile/v1/classifieds/UploadImage")
Call<JsonElement> UploadImage(@Part MultipartBody.Part body,@Part("uniqId")String uniqId,@Part("token")String token);}
建設者:
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitV2Config {
private static Retrofit getRetrofit(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.qrz.ru")
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
public static Retrofitv2Api getService(){
return getRetrofit().create(Retrofitv2Api.class);
}
,並上傳片段:
String photoPath = photoAdapter.getPhotoPaths().get(0);
File image = new File(photoPath);
RequestBody imageBody = RequestBody.create(MediaType.parse("image/*"), image);
MultipartBody.Part body = MultipartBody.Part.createFormData("file", image.getName(), imageBody);
RetrofitV2Config.getService().UploadImage(body , "2536654", Token.getSavedToken()).enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
Log.i(Logger.TAG, "onResponseUpload " + response.body() + " " + response.code());
}
@Override
public void onFailure(Call<JsonElement> call, Throwable t) {
Log.i(Logger.TAG, "onFailureUpload");
}
});
後端回覆:
21:34:32.386117 IP 46.37.203.69.50461 > 62.181.46.35.http: Flags [P.], seq 1184070465:1184071311, ack 3418438796, win 65535, length 846
[email protected]%.E>..#...PF.{A..<.P...]...POST /mobile/v1/classifieds/UploadImage HTTP/1.1
Content-Type: multipart/form-data
Content-Length: 647
Host: api.qrz.ru
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.2.0
--950f42dd-12a1-4e11-a3b0-826ddaf41aa3
Content-Disposition: form-data; name="img"
Content-Transfer-Encoding: binary
Content-Type: application/json; charset=UTF-8
Content-Length: 2
{}
--950f42dd-12a1-4e11-a3b0-826ddaf41aa3
Content-Disposition: form-data; name="uniqId"
Content-Transfer-Encoding: binary
Content-Type: application/json; charset=UTF-8
Content-Length: 8
"215132"
--950f42dd-12a1-4e11-a3b0-826ddaf41aa3
Content-Disposition: form-data; name="token"
Content-Transfer-Encoding: binary
Content-Type: application/json; charset=UTF-8
Content-Length: 22
請仔細閱讀http://stackoverflow.com/questions/36491096/retrofit-multipart-request-required-multipartfile-parameter -file - 是 - 不預/ 36514662#36514662 – BNK