问题
My server returns simple Json result like below,
{"message":"Upload Success.!"}
I am trying to get the result into Retrofit Model class
public class MyResponse {
@SerializedName("message")
String message;
}
My interface class is here
public interface MyService {
@Multipart
@POST("/")
public retrofit2.Call<MyResponse> saveFile(@Part("filename") String fileName, @Part("photo") RequestBody photo);
}
this is how i try to fetch the result
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://xxxxxxx.com/fileupload.php/").addConverterFactory(GsonConverterFactory.create(gson)).build();
MyService service = retrofit.create(MyService.class);
retrofit2.Call<MyResponse> call = service.saveFile(filename, requestbody);
retrofit2.Response<MyResponse> response = call.execute();
But i cannot bind the result to the MyResponse
class
The result it
02-23 21:22:43.074 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
02-23 21:22:43.074 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
02-23 21:22:43.074 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
02-23 21:22:43.074 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
02-23 21:22:43.074 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:117)
02-23 21:22:43.074 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
02-23 21:22:43.074 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
02-23 21:22:43.074 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:89)
02-23 21:22:43.074 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: at retrofitquictest.com.retrofitquicktest.MainActivity$1.run(MainActivity.java:72)
02-23 21:22:43.075 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: at java.lang.Thread.run(Thread.java:818)
02-23 21:22:43.075 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
02-23 21:22:43.075 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
02-23 21:22:43.075 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
02-23 21:22:43.075 20293-20410/retrofitquictest.com.retrofitquicktest W/System.err: ... 8 more
I guess I'm doing small mistake here. can someone identify that please ?
回答1:
According to retrofit documentation your base url should only consist the first part of url in your case:
Base url should be "http://xxxxxxx.com/
and add the other part in Post like this @POST("fileupload.php/")
. Give it a try maybe it will work for you.
Thanks!
回答2:
please check response in PostMan, if in postman you get same response as you told then this means that in your app you are getting error response which is in string format, thats why you are getting this type mismatch exception.
来源:https://stackoverflow.com/questions/42430403/retrofit-android-basic-and-simple-issue