Android:Retrofit 2和GSON&的问题JSON

时间:2016-08-26 14:10:59

标签: android json gson retrofit2 flickr

我现在尝试解析此链接中的JSON(" https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=fdac2e9676991ac53b34651adab52518&format=json&nojsoncallback=1&auth_token=72157671978046542-6e266595ffed01f8&api_sig=58e08d365779a8f2e946a2b5320199e2")

这就是我在Java中所做的:

package com.example.karim.bluecrunch;

import com.google.gson.annotations.SerializedName;

public class SinglePhotoRetrofit {
@SerializedName("id")
public String id;
@SerializedName("owner")
public String owner;
@SerializedName("secret")
public String secret;
@SerializedName("server")
public String server;
@SerializedName("farm")
  public int farm;     

    @SerializedName("title")
    public String title;

    @SerializedName("ispublic")
    public int ispublic;

    @SerializedName("isfriend")
    public int isfriend;

    @SerializedName("isfamily")
    public int isfamily;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

package com.example.karim.bluecrunch;

import com.google.gson.annotations.SerializedName;     
import java.util.ArrayList;
import java.util.List;

/**
 * Created by karim on 8/26/16.
 */
public class PhotosRetrofit {
    @SerializedName("page")
    public int page;

    @SerializedName("pages")
    public int pages;

    @SerializedName("perpage")
    public int perpage;

    @SerializedName("total")
    public String total;

    @SerializedName("photo")
    public List<SinglePhotoRetrofit> photo;

    @SerializedName("stat")
    public String stat;

    public List<SinglePhotoRetrofit> getPhoto() {
        return photo;
    }

    public void setPhoto(List<SinglePhotoRetrofit> photo) {
        this.photo = photo;
    }

    public String getTotal() {
        return total;
    }

    public void setTotal(String total) {
        this.total = total;
    }
}`

这是我的界面

package com.example.karim.bluecrunch;

import java.util.List;    
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

/**
 * Created by karim on 8/26/16.
 */
public interface HandleRetrofit {
/*
https://api.flickr.com/services/rest/?method=flickr.photos.search
&api_key=6d5c5a20d108f8f56f324394d3e2381f
&format=json
&nojsoncallback=1
&auth_token=72157672948729705-c211cbcbcac8bb30
&api_sig=bd73bb34b0f29390a80c6ffdbb376c97

 */
    @GET("rest/?")
    Call<PhotosRetrofit> Photos (
            @Query("method") String method,
            @Query("api_key") String key,
            @Query("format") String format,
            @Query("nojsoncallback") int call_back,
            @Query("auth_token") String token,
            @Query("api_sig") String sig
    );

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.flickr.com/services/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
}

我的MainActivity看起来像->

package com.example.karim.bluecrunch;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

                /*
https://api.flickr.com/services/rest/?method=flickr.photos.search
&api_key=fdac2e9676991ac53b34651adab52518
&format=json&nojsoncallback=1
&auth_token=72157671978046542-6e266595ffed01f8
&api_sig=58e08d365779a8f2e946a2b5320199e2
 */
                final String API_KEY = "fdac2e9676991ac53b34651adab52518";
                final String METHOD = "flickr.photos.search";
                final String AUTH_TOKEN = "72157671978046542-6e266595ffed01f8";
                final String API_SIG = "58e08d365779a8f2e946a2b5320199e2";
                final String FORMAT = "json";
                final int CALL_BACK = 1;

        HandleRetrofit handleRetrofit = HandleRetrofit.retrofit.create(HandleRetrofit.class);
        Call<PhotosRetrofit> call = handleRetrofit.Photos(METHOD,API_KEY,FORMAT,CALL_BACK,AUTH_TOKEN,API_SIG);
        call.enqueue(new Callback<PhotosRetrofit>() {
                    @Override
                    public void onResponse(Call<PhotosRetrofit> call, Response<PhotosRetrofit> response) {
                        Log.d("MainActivity", "Status Code = " + response.code());
                        TextView textView = (TextView) findViewById(R.id.photoTitle);

                        PhotosRetrofit photosRetrofit = response.body();

                        textView.setText(photosRetrofit.total+"\n");    

                        Log.w("---___---Respones","Hereeeeeeeeeeeeeeeeeeee");
                    }   

                    @Override
                    public void onFailure(Call<PhotosRetrofit> call, Throwable t) {
                        Toast.makeText(MainActivity.this,"Error :"+t.getMessage(),Toast.LENGTH_LONG).show();
                        Log.w("---___--- Error ",t.getMessage());
                    }
                });    
    }
}

它在我的TextView中返回null,我也越来越多地尝试检索不同的数据,但我已经失败了。

2 个答案:

答案 0 :(得分:1)

创建另一个类:

public class Photos{
  public PhotosRetrofit photos;
}

更新您的界面:

 Call<Photos> Photos (...)

更新活动:

Call<Photos> call = handleRetrofit.Photos(METHOD,API_KEY,FORMAT,CALL_BACK,AUTH_TOKEN,API_SIG);
call.enqueue(new Callback<Photos>() {
    @Override
    public void onResponse(Call<Photos> call, Response<Photos> response) {
        TextView textView = (TextView) findViewById(R.id.photoTitle);
        PhotosRetrofit photosRetrofit = response.body().photos;
        textView.setText(photosRetrofit.total+"\n");
    ....

答案 1 :(得分:0)

使用@Query将参数添加到网址。这会导致双参数,并且API不接受/理解您的请求。要么有一个不包含参数的网址,要么使用@Path

请参阅&#34;网址操作&#34; http://square.github.io/retrofit/

编辑:想想我误解了。您应该使用@Query但不包括查询路径 例如:

@GET("rest/?&format=json&nojsoncallback=1")
Call<PhotosRetrofit> Photos (
    @Query("method") String method,
    @Query("api_key") String key,
    @Query("auth_token") String token,
    @Query("api_sig") String sig
);

@Query值应该是查询路径应包含的确切值。