Java 使用改型获得JSON
我目前正在尝试使用改造来构建一个Muzei扩展,就像Roman在他的例子中使用的那样 我真的无法理解改装,但这就是我所拥有的 ArtSource.javaJava 使用改型获得JSON,java,android,json,retrofit,muzei,Java,Android,Json,Retrofit,Muzei,我目前正在尝试使用改造来构建一个Muzei扩展,就像Roman在他的例子中使用的那样 我真的无法理解改装,但这就是我所拥有的 ArtSource.java public class ArtSource extends RemoteMuzeiArtSource { private static final String TAG = "The Muzei Collection"; private static final String SOURCE_NAME = "The Muzei
public class ArtSource extends RemoteMuzeiArtSource {
private static final String TAG = "The Muzei Collection";
private static final String SOURCE_NAME = "The Muzei Collection";
private static final int ROTATE_TIME_MILLIS = 3 * 60 * 60 * 1000; // rotate every 3 hours
public ArtSource() {
super(SOURCE_NAME);
}
@Override
public void onCreate() {
super.onCreate();
setUserCommands(BUILTIN_COMMAND_ID_NEXT_ARTWORK);
}
@Override
protected void onTryUpdate(int reason) throws RetryException {
String currentToken = (getCurrentArtwork() != null) ? getCurrentArtwork().getToken() : null;
RestAdapter restAdapter = new RestAdapter.Builder()
.setServer("http://elliothesp.co.uk")
.build();
ArtService service = restAdapter.create(ArtService.class);
PhotosResponse response = service.getPopularPhotos();
if (response == null || response.photos == null) {
throw new RetryException();
}
if (response.photos.size() == 0) {
Log.w(TAG, "No photos returned from API.");
scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
return;
}
Random random = new Random();
Photo photo;
String token;
while (true) {
photo = response.photos.get(random.nextInt(response.photos.size()));
token = Integer.toString(photo.id);
if (response.photos.size() <= 1 || !TextUtils.equals(token, currentToken)) {
break;
}
}
publishArtwork(new Artwork.Builder()
.title(photo.title)
.byline(photo.user)
.imageUri(Uri.parse(photo.url))
.token(token)
.viewIntent(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com")))
.build());
scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
}
}
interface ArtService {
@GET("/muzei.php")
PhotosResponse getPopularPhotos();
static class PhotosResponse {
List<Photo> photos;
}
static class Photo {
int id;
String user;
String title;
String url;
}
}
公共类ArtSource扩展了RemoteMuzeiArtSource{
私有静态最终字符串标记=“Muzei集合”;
私有静态最终字符串SOURCE_NAME=“Muzei集合”;
私有静态最终整数旋转\u时间\u毫秒=3*60*60*1000;//每3小时旋转一次
公共艺术资源(){
超级(来源名称);
}
@凌驾
public void onCreate(){
super.onCreate();
setUserCommands(内置命令\u ID\u下一个\u图形);
}
@凌驾
受保护的无效onTryUpdate(int原因)引发RetryException{
字符串currentToken=(getCurrentArtwork()!=null)?getCurrentArtwork().getToken():null;
RestAdapter RestAdapter=new RestAdapter.Builder()
.setServer(“http://elliothesp.co.uk")
.build();
ArtService=restAdapter.create(ArtService.class);
photoResponse=service.getPopularPhotos();
if(response==null | | response.photos==null){
抛出新的RetryException();
}
if(response.photos.size()==0){
w(标签“没有从API返回照片”);
scheduleUpdate(System.currentTimeMillis()+ROTATE\u TIME\u MILLIS);
返回;
}
随机=新随机();
照片;
字符串标记;
while(true){
photo=response.photos.get(random.nextInt(response.photos.size());
token=Integer.toString(photo.id);
如果(response.photos.size()很好,JSON
响应的格式不适合您的界面,则应为:
interface service {
@GET("/muzei.php")
Map<String,Photo> getPopularPhotos();
static class Photo {
int id;
String user;
String title;
String url;
}
}
然后在得到response
后重新编写代码,对其进行迭代那么,JSON
格式的响应
不适合您的界面,它应该是:
interface service {
@GET("/muzei.php")
Map<String,Photo> getPopularPhotos();
static class Photo {
int id;
String user;
String title;
String url;
}
}
然后,在获得response
后重新编写代码,对其进行迭代这是整个日志吗?以及ArtSource.java(未完全列出)第62行的内容。此外,您还可以尝试将接口publicphoto=response.photos.get(random.nextInt(response.photos.size())设置为publicphoto=response.photos.get(random.nextInt(response.photos.size());顺便说一句,从500px.com api json中提取的示例如下:这是整个日志吗?以及ArtSource.java(未完全列出)第62行中的内容。您还可以尝试将接口publicphoto=response.photos.get(random.nextInt(response.photos.size())设置为publicphoto=response.photos.get(random.nextInt(response.photos.size());顺便说一句,500px.com api json的示例如下:谢谢!我有一种感觉,这就是问题所在,但我不太明白。你的回答很清楚。谢谢!我有一种感觉,这就是问题所在,但我不太明白。你的回答很清楚