【发布时间】:2014-09-03 04:53:43
【问题描述】:
我一直对在我的应用程序中实现 Retrofit 以发出简单的 POST 和 GET 请求感到好奇。但是,我在解决这个问题时遇到了问题。今天我决定尝试将它集成到我的应用程序中,但遇到了麻烦。如果你能看看我的代码,那就太好了。这是我得到的错误
“原因:retrofit.RetrofitError:retrofit.converter.ConversionException:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_ARRAY,但在第1行第2列是BEGIN_OBJECT”
如果您想尝试一下,这是 api 调用。 http://api.thebookofeveryone.com/social/makelove?phrase=love
我也知道我现在没有对返回的图像对象做任何事情,因为我需要先解决这个问题。
谢谢。
主活动
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
extraThread task = new extraThread();
task.execute();
}
public class extraThread extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
RetrofitInterface.getImageApiClient().getImage("love");
return null;
}
}
RetroFit 界面
public class RetrofitInterface {
private static ImageApiInterface sImageService;
public static ImageApiInterface getImageApiClient() {
if (sImageService == null) {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://api.thebookofeveryone.com")
.build();
sImageService = restAdapter.create(ImageApiInterface.class);
}
return sImageService;
}
public interface ImageApiInterface {
@GET("/social/makelove")
Image getImage(@Query("phrase") String phrase);
}
}
图像类
public class Image {
boolean success;
String url;
}
【问题讨论】: