Retrofit2,onResponse
方法不会在其主体中执行代码。该应用程序不会崩溃,但列表未显示活动中的数据。我创建log.e()
以查看我的代码在哪里执行而不是在执行。on 响应中的代码根本没有执行,但onFailure
确实执行了。
我懂了 :
"java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ " in the logcat.
我的界面
public interface RadioApiClient {
String BASE_URL = "https://api.broadcastify.com/audio/";
String API_KEY = "1234567890";//dummy key
String JSON_RESPONSE = "json";
String XML_RESPONSE = "XML";
//api.broadcastify.com/audio/?a=feeds&type=json&key=1234567890
@GET(".")
Call<List<Feeds>> getFeeds(@Query("a") String action,
@Query("type") String reponseType,
@Query("key") String key);
@GET("api.broadcastify.com/audio/?a=feeds&type=json&key=1234567890")
Call<List<Feeds>> getFeeds();
}
我的主要活动
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
final Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(RadioApiClient.BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
RadioApiClient client = retrofit.create(RadioApiClient.class);
//I checked if internet was connected
if (isNetworkConnected()) {
Log.e("success", "" + "Has internet");
} else {
Log.e("No success", "" + "no internet");
}
Call<List<Feeds>> call = client.getFeeds("feeds",
RadioApiClient.JSON_RESPONSE,
RadioApiClient.API_KEY);
isNetworkConnected();
Log.e("Accessing url", client.getFeeds("feeds",
RadioApiClient.JSON_RESPONSE,
RadioApiClient.API_KEY).request().url().toString());
//all log.e excecuted apart from the one in the onResponse method
call.enqueue(new Callback<List<Feeds>>() {
@Override
public void onResponse(Call<List<Feeds>> call, Response<List<Feeds>> response) {
Log.e("success", "" + "3");
List<Feeds> feeds = response.body();
Context context = getApplicationContext();
Log.e("success", "" + "4");
Log.e("Feedsize", "" + feeds.size());
Toast toast = Toast.makeText(context, feeds.size(), Toast.LENGTH_LONG);
toast.show();
listView.setAdapter(new ArrayAdapter<Feeds>(MainActivity.this, android.R.layout.simple_list_item_1, feeds));
}
@Override
public void onFailure(Call<List<Feeds>> call, Throwable t) {
Log.e("no success", "" + "onfailure");
t.printStackTrace();
}
});
}
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
}
我的模特
public class Radio {
@SerializedName("Feeds")
@Expose
private List<Feeds> feeds = null;
public List<Feeds> getFeeds() {
return feeds;
}
public void setFeeds(List<Feeds> feeds) {
this.feeds = feeds;
}
}
public class Feeds {
@SerializedName("id")
@Expose
private int id;
@SerializedName("status")
@Expose
private int status;
@SerializedName("listeners")
@Expose
private int listeners;
@SerializedName("descr")
@Expose
private String descr;
@SerializedName("genre")
@Expose
private String genre;
@SerializedName("mount")
@Expose
private String mount;
@SerializedName("bitrate")
@Expose
private int bitrate;
@SerializedName("Counties")
@Expose
private List<County> counties = null;
@SerializedName("Relays")
@Expose
private List<Relay> relays = null;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public int getListeners() {
return listeners;
}
public void setListeners(int listeners) {
this.listeners = listeners;
}
public String getDescr() {
return descr;
}
public void setDescr(String descr) {
this.descr = descr;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getMount() {
return mount;
}
public void setMount(String mount) {
this.mount = mount;
}
public int getBitrate() {
return bitrate;
}
public void setBitrate(int bitrate) {
this.bitrate = bitrate;
}
public List<County> getCounties() {
return counties;
}
public void setCounties(List<County> counties) {
this.counties = counties;
}
public List<Relay> getRelays() {
return relays;
}
public void setRelays(List<Relay> relays) {
this.relays = relays;
}
}