问题
I'm trying to get just string request but it's give error like this
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
And my API output is like this :
{
"status": true,
"message": "Video uploaded successfully!",
"data": {
"video_name": "674631516178278_abc2.mp4",
"video_thumbnail": "674631516178278_thumb0017.jpg"
}
}
Code for response :
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
if (response.body() != null)
Log.e("UploadResponse>>>", response.body());
layoutUpload.setVisibility(View.GONE);
if (file.exists()) {
file.delete();
thumb.delete();
}
} catch (Exception e) {
e.printStackTrace();
layoutUpload.setVisibility(View.GONE);
if (file.exists()) {
file.delete();
thumb.delete();
}
}
}
回答1:
Try like this
@Override
public void onResponse(Call<ResponseBody> call, Response< ResponseBody > response) {
try {
if (response.body() != null)
Log.e("UploadResponse>>>", response.body());
layoutUpload.setVisibility(View.GONE);
if (file.exists()) {
file.delete();
thumb.delete();
}
} catch (Exception e) {
e.printStackTrace();
layoutUpload.setVisibility(View.GONE);
if (file.exists()) {
file.delete();
thumb.delete();
}
}
}
回答2:
.addConverterFactory(ScalarsConverterFactory.create()) .addxxxx
回答3:
I also faced similar issue. The answer provided by Dattatray Nande i.e. simply adding ScalarsConverterFactory.create() in Retrofit.Builder helped.
Additionally don't forget to add content-type as shown below to your interface
@Headers("Content-Type: text/html")
@GET("/apiMethod")
Call<String> YourAPIFunction();
回答4:
Error say's you want to get result in String body. If you want to do this, Just add ScalarsConverterFactory.create() in your Retrofit.Builder.
public static Retrofit getClient() {
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(getBaseUrl())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
return retrofit;
}
Use retrofit Implementation in app level build.gradle.
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
来源:https://stackoverflow.com/questions/48296987/retrofit-expected-a-string-but-was-begin-object-at-line-1-column-2-path