【问题标题】:Retrofit,onResponse method doesnt execute code in its body改造,onResponse 方法不会在其主体中执行代码
【发布时间】:2017-03-31 06:32:05
【问题描述】:

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;
        }

    }

【问题讨论】:

  • stacktrace 中打印了什么?
  • 您可以尝试添加日志记录并查看请求是否实际发出/以及您是否从服务器获得响应:futurestud.io/tutorials/retrofit-2-log-requests-and-responses
  • 我得到:java.lang.IllegalStateException:预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $ 处是 BEGIN_OBJECT

标签: android listview retrofit2 jsonresponse


【解决方案1】:

将 RadioApiClient 中的代码更改为此

public interface RadioApiClient {

String BASE_URL = "https://api.broadcastify.com/";
String API_KEY = "1234567890";//dummy key
String JSON_RESPONSE = "json";
String XML_RESPONSE = "XML";

@GET("audio/?")
Call<List<Feeds>> getFeeds(
        @Query("a") String action,
        @Query("type") String responseType,
        @Query("key") String key);

}

【讨论】:

【解决方案2】:

我发现出了什么问题。我有一个无线电类,它有一个返回提要列表的方法。我使用的是无线电提要对象而不是返回提要数组的无线电对象。这就是错误的原因:“预期为 BEGIN_ARRAY,但为 BEGIN_OBJECT”即将出现

【讨论】:

    猜你喜欢
    • 2020-07-04
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多