java - 尝试通过改造解析 JSON Feed 时出现 IllegalStateException

标签 java android json android-recyclerview retrofit

我正在尝试使用 JSON 和 Retrofit 库在 Recyclerview 中通过 CardViews 显示我的提要。 我已经解决这个问题 4 个小时,现在在 google 上搜索 1 个小时,但仍然没有成功。

无论我尝试什么,我都会收到以下错误:

D/错误:java.lang.IllegalStateException:预期为 BEGIN_OBJECT,但在第 1 行第 2 列路径 $ 处为 BEGIN_ARRAY D/错误:java.lang.IllegalStateException:预期为 BEGIN_OBJECT,但在第 1 行第 2 列路径 $

处为 BEGIN_ARRAY <小时/>

PHP API 的 JSON 示例:

[{"id":"143","uploadID":"36","type":"excellent","userID":"58","username":"doitlikeaves","date":"31 May","timestamp":"2016-05-31 22:37:50","title":"PH Zusammenfassung #2","subject":"Physik"},{"id":"142","uploadID":"36","type":"presentation","userID":"58","username":"doitlikeaves","date":"31 May","timestamp":"2016-05-31 21:57:21","title":"PH Zusammenfassung #2","subject":"Physik"},{"id":"141","uploadID":"61","type":"document","userID":"56","username":"maja","date":"31 May","timestamp":"2016-05-31 14:29:00","title":" Rev.","subject":"Geschichte"}]
<小时/> <小时/>

Android 文件:

FeedElement.js:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class FeedElement {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("uploadID")
    @Expose
    private String uploadID;
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("userID")
    @Expose
    private String userID;
    @SerializedName("username")
    @Expose
    private String username;
    @SerializedName("date")
    @Expose
    private String date;
    @SerializedName("timestamp")
    @Expose
    private String timestamp;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("subject")
    @Expose
    private String subject;

    /**
     *
     * @return
     * The id
     */
    public String getId() {
        return id;
    }

    /**
     *
     * @param id
     * The id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     *
     * @return
     * The uploadID
     */
    public String getUploadID() {
        return uploadID;
    }

    /**
     *
     * @param uploadID
     * The uploadID
     */
    public void setUploadID(String uploadID) {
        this.uploadID = uploadID;
    }

    /**
     *
     * @return
     * The type
     */
    public String getType() {
        return type;
    }

    /**
     *
     * @param type
     * The type
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     *
     * @return
     * The userID
     */
    public String getUserID() {
        return userID;
    }

    /**
     *
     * @param userID
     * The userID
     */
    public void setUserID(String userID) {
        this.userID = userID;
    }

    /**
     *
     * @return
     * The username
     */
    public String getUsername() {
        return username;
    }

    /**
     *
     * @param username
     * The username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     *
     * @return
     * The date
     */
    public String getDate() {
        return date;
    }

    /**
     *
     * @param date
     * The date
     */
    public void setDate(String date) {
        this.date = date;
    }

    /**
     *
     * @return
     * The timestamp
     */
    public String getTimestamp() {
        return timestamp;
    }

    /**
     *
     * @param timestamp
     * The timestamp
     */
    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    /**
     *
     * @return
     * The title
     */
    public String getTitle() {
        return title;
    }

    /**
     *
     * @param title
     * The title
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     *
     * @return
     * The subject
     */
    public String getSubject() {
        return subject;
    }

    /**
     *
     * @param subject
     * The subject
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

}
<小时/>

JSONResponseFeed.js:

import java.util.ArrayList;
public class JSONResponseFeed {
    private ArrayList<FeedElement> feeds;

    /**
     * @return The feeds
     */
    public ArrayList<FeedElement> getFeed() {
        return feeds;
    }
}
<小时/>

RequestInterfaceFeed.js:

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface RequestInterfaceFeed {
    @GET("getFeed")
    Call<JSONResponseFeed> getJSON(@Query("key") String apiKey);
}
<小时/>

DataAdapterFeed.js:

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class DataAdapterFeed extends RecyclerView.Adapter<DataAdapterFeed.ViewHolder> {
    private List<FeedElement> feed;

public DataAdapterFeed(List<FeedElement> feed) {
    this.feed = feed;
}

@Override
public DataAdapterFeed.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(DataAdapterFeed.ViewHolder viewHolder, int i) {

    viewHolder.tv_name.setText(feed.get(i).getUsername());
}

@Override
public int getItemCount() {
    return feed.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    private TextView tv_name;
    public ViewHolder(View view) {
        super(view);

        tv_name = (TextView)view.findViewById(R.id.username);

        }
    }
}
<小时/>

MainActivity.js 中的函数:

 private void loadJSON(){
    final String url = "https://mydomainiscorrect.justreplacedithere/api/v1/";
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    final RequestInterfaceFeed request = retrofit.create(RequestInterfaceFeed.class);
    Call<JSONResponseFeed> call = request.getJSON(apiKey);
    call.enqueue(new Callback<JSONResponseFeed>() {
        @Override
        public void onResponse(Call<JSONResponseFeed> call, retrofit2.Response<JSONResponseFeed> response) {
            List<FeedElement> data = response.body().getFeed();
            adapter = new DataAdapterFeed(data);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<JSONResponseFeed> call, Throwable t) {
            Log.d("Error", t.getMessage());
        }
    });
}

private void initViews(){
    recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);
    loadJSON();
}
<小时/>

如果需要 - cardView.xml:

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/username"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold" />
</LinearLayout>

<小时/>

MainActivity RecyclerView XML:

 <android.support.v7.widget.RecyclerView
        android:id="@+id/card_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
<小时/> <小时/>

我对 Java 和 Android 编程还很陌生,我对 PHP 很了解,但 Java 有时确实让我头疼。

非常感谢您对我的帮助,我真的很感激!

最佳答案

不需要 JSONResponseFeed。更新RequestInterfaceFeed

public interface RequestInterfaceFeed {
    @GET("getFeed")
    Call<List<FeedElement>> getJSON(@Query("key") String apiKey);
}

并更新loadJSON方法

private void loadJSON(){
    final String url = "https://mydomainiscorrect.justreplacedithere/api/v1/";
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    final RequestInterfaceFeed request = retrofit.create(RequestInterfaceFeed.class);
    Call<List<FeedElement>> call = request.getJSON(apiKey);
    call.enqueue(new Callback<List<FeedElement>>() {
        @Override
        public void onResponse(Call<List<FeedElement>> call, retrofit2.Response<List<FeedElement>> response) {
            List<FeedElement> data = response.body();
            adapter = new DataAdapterFeed(data);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<List<FeedElement>> call, Throwable t) {
            Log.d("Error", t.getMessage());
        }
    });
}

关于java - 尝试通过改造解析 JSON Feed 时出现 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38574193/

相关文章:

java - 没有打印语句,循环看不到其他线程更改的值

c# - Unity 游戏不适合 Android 屏幕

c# - 从 ASP.NET MVC 项目中的 C# 代码隐藏向另一个网站发送 JSON 请求

javascript - 为什么我必须在 Angular 中以特殊方式访问 JSON?

javascript - 如何删除存储在本地存储中的数据?

java - 具有共享代码依赖项的 Maven 独立 jar

java - 如何判断 Graphics.drawImage() 何时实际完成

android - 如何使用 ShareCompat 共享多个文件

java - 使用@Async 方法的 JUnit 回滚事务

java - 从当前位置向后循环遍历数组