Android:Retrofit 2 和 GSON 的问题 & JSON
Android : Issues with Retrofit 2 and GSON & JSON
中解析 JSON
这就是我在 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());
}
});
}
}
它 returns 在我的 TextView 中为 null,我也尝试了越来越多的方法来检索不同的数据,但我都失败了。
使用 @Query
将参数添加到 url。这会导致双参数和 API 而不是 accepting/understanding 您的请求。要么使用不包含参数的 URL,要么使用 @Path
.
请参阅 "URL Manipulation" 下的文档
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 值应该是查询路径应该包含的准确值。
再创建一个class:
public class Photos{
public PhotosRetrofit photos;
}
更新您的界面:
Call<Photos> Photos (...)
更新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");
....
这就是我在 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());
}
});
}
}
它 returns 在我的 TextView 中为 null,我也尝试了越来越多的方法来检索不同的数据,但我都失败了。
使用 @Query
将参数添加到 url。这会导致双参数和 API 而不是 accepting/understanding 您的请求。要么使用不包含参数的 URL,要么使用 @Path
.
请参阅 "URL Manipulation" 下的文档 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 值应该是查询路径应该包含的准确值。
再创建一个class:
public class Photos{
public PhotosRetrofit photos;
}
更新您的界面:
Call<Photos> Photos (...)
更新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");
....