Reputation: 35
I'm now try to parse JSON FROM this link ("https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=fdac2e9676991ac53b34651adab52518&format=json&nojsoncallback=1&auth_token=72157671978046542-6e266595ffed01f8&api_sig=58e08d365779a8f2e946a2b5320199e2")
This is what I do in my 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;
}
}
and
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;
}
}`
and here is my interface
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();
}
and my MainActivity looks like ->
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());
}
});
}
}
It returns null in my TextView, also I've tried more and more to retrieve different data but I've failed on that.
Upvotes: 0
Views: 1367
Reputation: 701
Create another one class:
public class Photos{
public PhotosRetrofit photos;
}
Update your interface:
Call<Photos> Photos (...)
Update activity:
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");
....
Upvotes: 1
Reputation: 313
Using @Query
adds parameters to the url. This results in double parameters and the API not accepting/understanding your request. Either have an URL that doesn't contain the parameters or use @Path
.
See in the docs under "URL Manipulation" http://square.github.io/retrofit/
EDIT: Think I misunderstood. You should use @Query but not include the query-paths themself For example:
@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
);
The @Query value should be the exact value that the query-path should contain.
Upvotes: 0