Ich bin jetzt versuchen, JSON über diesen Link zu analysieren ("https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=fdac2e9676991ac53b34651adab52518&format=json&nojsoncallback=1&auth_token=72157671978046542-6e266595ffed01f8&api_sig=58e08d365779a8f2e946a2b5320199e2")Android: Probleme mit Retrofit 2 und Gson & JSON
Dies ist, was ich in meinem Java tun:
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;
}
}
und
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;
}
}`
und hier ist meine Schnittstelle
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();
}
und meine MainActivity sieht aus wie ->
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());
}
});
}
}
es in meinem Textview null zurück, ich habe auch versucht, mehr und mehr auf verschiedene Daten abrufen, aber ich habe auf das schlägt fehl.
Diese Frage kann derzeit nicht bearbeitet werden, aus diesem Grund: 'Bei" Links Bei ideone.com muss Code beigefügt werden, bitte den gesamten Code mit der Code-Toolbar oder der Tastenkombination STRG + K um 4 Leerzeichen einrücken tcut. Für weitere Bearbeitungshilfe klicken Sie auf das Symbol [?] In der Symbolleiste. "'. Bitte bearbeiten Sie die Frage. – halfer
(Um zu verdeutlichen, fügen Sie bitte Ihren Code _to_ die Frage, anstatt auf Links auf einer Pasteboard zeigen. Der Inhaltsfilter gibt es für eine Grund, und ist nicht zum Umgehen) – halfer
Es ist verdrahtet! Ich ziehe es vor, meine Codes in Verbindungen hinzuzufügen, weil es ihnen mehr klar als das Einfügen sie hier zeigt! Also, warum Stackoverflow erzwingen Sie mich auf das !! –