gpt4 book ai didi

java - Retrofit 中未调用 onResponse 方法?

转载 作者:行者123 更新时间:2023-12-02 12:06:05 26 4
gpt4 key购买 nike

我正在使用 RetroFit 创建一个应用程序。我从 URL 获取 JSON 字符串数据,并使用 RetroFit 将其填充到回收 View 中。

我的问题

我的问题是,当应用程序执行时,它显示以下错误::

E/RecyclerView: No adapter attached; skipping layout
E/RecyclerView: No adapter attached; skipping layout
D/Error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

我的检查

  • 检查了网址
  • 使用浏览器检查 URL。成功返回 json

我努力尝试解决这个问题,但没有解决。

我手动运行json url并成功返回JSON数据。但在程序上,它返回错误。

JSON 数据示例

[
{
"user_id": "59e4897140d1666c6e8b4567",
"post_id": "59eeb10d40d16686168b4567",
"post_url": "http://demo.cogzideltemplates.com/anan/posts/images/22f3fb0b974be4968118a410e0ad48f7.jpg",
"post_type": "image",
"caption": "vvnnk",
"created": 1508815117,
"time_diff": "1 hours ago",
"first_name": null,
"last_name": null,
"user_name": null,
"email": null,
"profile_pic": "http://demo.cogzideltemplates.com/anan/images/users/no_avatar.jpg",
"category": "New release"
},
{
"user_id": "59e4897140d1666c6e8b4567",
"post_id": "59ee306940d166697f8b4567",
"post_url": "http://demo.cogzideltemplates.com/anan/posts/images/8f81bd77e60e596cf458d42a743897f8.jpg",
"post_type": "image",
"caption": "cutyy",
"created": 1508782185,
"time_diff": "10 hours ago",
"first_name": null,
"last_name": null,
"user_name": null,
"email": null,
"profile_pic": "http://demo.cogzideltemplates.com/anan/images/users/no_avatar.jpg",
"category": "Super Star"
}
]

Post.java(JSON 的 POJO)

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

public class Post {

@SerializedName("user_id")
@Expose
private String userId;
@SerializedName("post_id")
@Expose
private String postId;
@SerializedName("post_url")
@Expose
private String postUrl;
@SerializedName("post_type")
@Expose
private String postType;
@SerializedName("caption")
@Expose
private String caption;
@SerializedName("created")
@Expose
private Integer created;
@SerializedName("time_diff")
@Expose
private String timeDiff;
@SerializedName("first_name")
@Expose
private Object firstName;
@SerializedName("last_name")
@Expose
private Object lastName;
@SerializedName("user_name")
@Expose
private Object userName;
@SerializedName("email")
@Expose
private Object email;
@SerializedName("profile_pic")
@Expose
private String profilePic;
@SerializedName("category")
@Expose
private String category;

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getPostId() {
return postId;
}

public void setPostId(String postId) {
this.postId = postId;
}

public String getPostUrl() {
return postUrl;
}

public void setPostUrl(String postUrl) {
this.postUrl = postUrl;
}

public String getPostType() {
return postType;
}

public void setPostType(String postType) {
this.postType = postType;
}

public String getCaption() {
return caption;
}

public void setCaption(String caption) {
this.caption = caption;
}

public Integer getCreated() {
return created;
}

public void setCreated(Integer created) {
this.created = created;
}

public String getTimeDiff() {
return timeDiff;
}

public void setTimeDiff(String timeDiff) {
this.timeDiff = timeDiff;
}

public Object getFirstName() {
return firstName;
}

public void setFirstName(Object firstName) {
this.firstName = firstName;
}

public Object getLastName() {
return lastName;
}

public void setLastName(Object lastName) {
this.lastName = lastName;
}

public Object getUserName() {
return userName;
}

public void setUserName(Object userName) {
this.userName = userName;
}

public Object getEmail() {
return email;
}

public void setEmail(Object email) {
this.email = email;
}

public String getProfilePic() {
return profilePic;
}

public void setProfilePic(String profilePic) {
this.profilePic = profilePic;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

}


DataAdapter.java

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<Post> post;

public DataAdapter(ArrayList<Post> post) {
this.post = post;
}

@Override
public DataAdapter.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(DataAdapter.ViewHolder viewHolder, int i) {

viewHolder.tv_userId.setText(post.get(i).getUserId());
viewHolder.tv_postId.setText(post.get(i).getPostId());
viewHolder.tv_postType.setText(post.get(i).getPostType());
viewHolder.tv_caption.setText(post.get(i).getCaption());

Picasso.with(viewHolder.tv_postUrl.getContext()).load(post.get(i).getPostUrl()).into(viewHolder.tv_postUrl);
Picasso.with(viewHolder.tv_profilePic.getContext()).load(post.get(i).getProfilePic()).into(viewHolder.tv_profilePic);

viewHolder.tv_timeDiff.setText(post.get(i).getTimeDiff());
viewHolder.tv_category.setText(post.get(i).getCategory());
viewHolder.tv_created.setText(post.get(i).getCreated());


}

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

public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_userId,tv_postId,tv_postType,tv_caption,tv_timeDiff,tv_category,tv_created;
ImageView tv_profilePic,tv_postUrl;
public ViewHolder(View view) {
super(view);

tv_userId = (TextView)view.findViewById(R.id.tv_userId);
tv_postUrl = (ImageView) view.findViewById(R.id.tv_postUrl);
tv_postType = (TextView)view.findViewById(R.id.tv_postType);
tv_caption = (TextView)view.findViewById(R.id.tv_caption);
tv_postId = (TextView)view.findViewById(R.id.tv_postId);



tv_timeDiff = (TextView)view.findViewById(R.id.tv_timeDiff);
tv_profilePic = (ImageView)view.findViewById(R.id.tv_profilePic);
tv_category = (TextView)view.findViewById(R.id.tv_category);
tv_created = (TextView)view.findViewById(R.id.tv_created);

}
}

}

JSONResponse.java

public class JSONResponse {
private Post[] post;

public Post[] getPost() {
return post;
}
}

RequestInterface.java

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

public interface RequestInterface {

@GET("anan/mobile/posts/viewall_Posts")
Call<JSONResponse> getJSON();
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private ArrayList<Post> data;
private DataAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
loadJSON();
}
private void loadJSON(){

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://demo.cogzideltemplates.com")
.addConverterFactory(GsonConverterFactory.create())
.build();

RequestInterface request = retrofit.create(RequestInterface.class);
Call<JSONResponse> call = request.getJSON();
System.out.println("enter the url: "+call.request().url());

call.enqueue(new Callback<JSONResponse>() {

@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {

JSONResponse jsonResponse = response.body();

data = new ArrayList<>(Arrays.asList(jsonResponse.getPost()));
adapter = new DataAdapter(data);
recyclerView.setAdapter(adapter);
}

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

}
});
}
}

这些是我的类(class)。请帮助我解决这个问题。

最佳答案

将 JSON 数据示例更改为 JSON 对象像这样

{
[
{
"user_id": "59e4897140d1666c6e8b4567",
"post_id": "59eeb10d40d16686168b4567",
"post_url": "http://demo.cogzideltemplates.com/anan/posts/images/22f3fb0b974be4968118a410e0ad48f7.jpg",
"post_type": "image",
"caption": "vvnnk",
"created": 1508815117,
"time_diff": "1 hours ago",
"first_name": null,
"last_name": null,
"user_name": null,
"email": null,
"profile_pic": "http://demo.cogzideltemplates.com/anan/images/users/no_avatar.jpg",
"category": "New release"
},
{
"user_id": "59e4897140d1666c6e8b4567",
"post_id": "59ee306940d166697f8b4567",
"post_url": "http://demo.cogzideltemplates.com/anan/posts/images/8f81bd77e60e596cf458d42a743897f8.jpg",
"post_type": "image",
"caption": "cutyy",
"created": 1508782185,
"time_diff": "10 hours ago",
"first_name": null,
"last_name": null,
"user_name": null,
"email": null,
"profile_pic": "http://demo.cogzideltemplates.com/anan/images/users/no_avatar.jpg",
"category": "Super Star"
}
]}

并更改您的JSONResponse.java

像这样

public class JSONResponse {
private List<Post> post;

public List<Post> getPost() {
return post;
}

}

Here is the tutorial for parsing JSON Array and JSON Object

关于java - Retrofit 中未调用 onResponse 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46902682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com