改造2,从API请求数据

时间:2019-03-11 16:03:42

标签: android retrofit2

我已经尝试了一段时间,以从Web Api请求数据。 Web API如下所示:

[ {
"data": [
{
"LastSeen": "10"
} ],
"Hit": false,
"message": "There you go",
"status": 200
} ]

我正在尝试从LastSeen请求数据,因此在这种情况下为10。 我使用的界面如下:

package quint.hotmail.com.clwo;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;

public interface JsonPlaceHolderApi {

    @GET("b/5c84fab65fe21458779c8131/2")
    Call<List<Comment>> getComments();
}

我也有一个注释类和一个基准类,这些类是使用http://www.jsonschema2pojo.org/制成的,外观如下(注释类):

public class Comment {

        @SerializedName("data")
        @Expose
    private List<Datum> data = null;
    @SerializedName("Hit")
    @Expose
    private Boolean hit;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("status")
    @Expose
    private Integer status;

    public List<Datum> getData() {
        return data;
    }

    public void setData(List<Datum> data) {
        this.data = data;
    }

    public Comment withData(List<Datum> data) {
        this.data = data;
        return this;
    }

    public Boolean getHit() {
        return hit;
    }

    public void setHit(Boolean hit) {
        this.hit = hit;
    }

    public Comment withHit(Boolean hit) {
        this.hit = hit;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Comment withMessage(String message) {
        this.message = message;
        return this;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Comment withStatus(Integer status) {
        this.status = status;
        return this;
    }

}

和基准类:

public class Datum {

@SerializedName("LastSeen")
@Expose
private String lastSeen;

public String getLastSeen() {
    return lastSeen;
}

public void setLastSeen(String lastSeen) {
    this.lastSeen = lastSeen;
}

public Datum withLastSeen(String lastSeen) {
    this.lastSeen = lastSeen;
    return this;
}

}

最后我有我的mainactivity类:

public class MainActivity extends AppCompatActivity {
private TextView textViewResult;
private TextView textViewResult2;

private JsonPlaceHolderApi jsonPlaceHolderApi;

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

    textViewResult = findViewById(R.id.textview);
    textViewResult2 = findViewById(R.id.textView2);


    Gson gson = new GsonBuilder()
            .setLenient()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
            .create();



    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.jsonbin.io/")
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();


    jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);

    getComments();
}

private void getComments() {



    Call<List<Comment>> call = jsonPlaceHolderApi
            .getComments();

    call.enqueue(new Callback<List<Comment>>() {
        @Override
        public void onResponse(Call<List<Comment>> call, Response<List<Comment>> response) {

            if (!response.isSuccessful()) {
                textViewResult.setText("Code: " + response.code());
                return;
            }
            List<Comment> comments = response.body();

            for (Comment comment : comments) {
                if(comment.getStatus() == 200){
                    ImageView firstimage = (ImageView) findViewById(R.id.imageView);
                    firstimage.setImageResource(R.drawable.fail);
                    textViewResult.append("Online");
                }
                else if(!(comment.getStatus() ==  200)){
                    String content = "";
                    content += ("Error code: ") + comment.getStatus();
                    textViewResult.append(content);
                    ImageView firstimage = (ImageView) findViewById(R.id.imageView);
                    firstimage.setImageResource(R.drawable.fail);

                }
            }
        }
        @Override
        public void onFailure(Call<List<Comment>> call, Throwable t) {
            textViewResult.setText(t.getMessage());
        }
    });
    }
   }

现在,我无法从api访问数据“ LastSeen”,我已经尝试了所有我能想象的东西,到目前为止,我还没有碰到运气。有谁可以帮助我访问此数据?

1 个答案:

答案 0 :(得分:-1)

for (Comment comment : comments) {
    if (comment.getData() != null && !comment.getData().isEmpty) {
       String lastSeen = comment.getData().get(0).getLastSeen()
       //'last seen' is your field
    }
}