gpt4 book ai didi

java.lang.IllegalStateException : Expected BEGIN_ARRAY but was BEGIN_OBJECT Android error 错误

转载 作者:行者123 更新时间:2023-11-29 18:30:17 25 4
gpt4 key购买 nike

首先,我已经尝试了很多答案,例如:

Expected BEGIN_ARRAY but was BEGIN_OBJECT retrofit2

retrofit error Expected BEGIN_ARRAY but was BEGIN_OBJECT

还有更多。

另外,我明白错误是说我返回的是 json 对象而不是 json 数组。

但是,仍然没有任何效果。

我正在尝试在我的 Android 应用程序中获取 WordPress 博客文章。我正在使用 WordPress 5.2.1 和 WP REST API 插件

RetrofitArrayApi.java

public interface RetrofitArrayApi {

@GET("wp-json/wp/v2/posts?per_page=100")
Call<List<WPPost>> getPostInfo();
/// to make call to dynamic URL
// @GET
// Call<List<WPPost>> getPostInfo(@Url String url);
//

}

是的,我删除了“列表”

JsonApiDataModel.java

public class JsonApiDataModel {

@SerializedName("media_details")
MediaDetails mediaDetails;

public static String getSourceUrl(String json) {


return new Gson().fromJson(json, JsonApiDataModel.class).mediaDetails.sizes.full.sourceUrl;


}

public class MediaDetails {
@SerializedName("sizes")
Sizes sizes;
}

public class Sizes {
// you can use full, medium or thumbnail here!
@SerializedName("full")
Full full;
}

public class Full {
@SerializedName("source_url")
String sourceUrl;
}

}

模型.java

public class Model {

public static final int IMAGE_TYPE =1;
public String title, subtitle, Image, date;
public int type;

public static final int TEXT_TYPE=0;

public String data;


public Model(int mtype, String mtitle, String msubtitle, String image, String mdate ){

this.title = mtitle;
this.date = mdate;
this.subtitle = msubtitle;
this.type = mtype;
this.Image = image;
}

public Model(int mtype){

this.type = mtype;
}
}

Blog.java

public class Blog extends Fragment {




private ProgressBar progressBar;
private String TAG ="BlogFragment";
private ArrayList<Model> list;
private RecyclerViewAdapter adapter;
private RecyclerView recyclerView;
private LinearLayoutManager mLayoutManager;
private String baseURL = "https://www.myfitbytes.com/";
public static final SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
public static List<WPPost> mListPost;
private boolean loading = true;
private int pastVisiblesItems, visibleItemCount, totalItemCount, PageCount=1, PageCountLock=0;






@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//LayoutInflater lf = getActivity().getLayoutInflater();
View view = lf.inflate(R.layout.fragment_blog, container, false);

//set fragment title
// getActivity().setTitle("Blog");


//wordpress blog posts

recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
progressBar = (ProgressBar) view.findViewById(R.id.progressBarPosts);

mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(mLayoutManager);

list = new ArrayList<Model>();
/// call retrofill
getRetrofit();

adapter = new RecyclerViewAdapter( list, getActivity());

recyclerView.setAdapter(adapter);




return view;



}



public void getRetrofit(){




Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create())
.build();

RetrofitArrayApi service = retrofit.create(RetrofitArrayApi.class);
Call<List<WPPost>> call = service.getPostInfo();

Log.e("hellooo", "Service = "+ service);
Log.e("hellooo", "retrofit = "+ retrofit);
Log.e("hellooo", "call = "+ call);

// to make call to dynamic URL

// String yourURL = yourURL.replace(BaseURL,"");
// Call<List<WPPost>> call = service.getPostInfo( yourURL);

/// to get only 6 post from your blog
// http://your-blog-url/wp-json/wp/v2/posts?per_page=2

// to get any specific blog post, use id of post
// http://www.blueappsoftware.in/wp-json/wp/v2/posts/1179

// to get only title and id of specific
// http://www.blueappsoftware.in/android/wp-json/wp/v2/posts/1179?fields=id,title



call.enqueue(new Callback<List<WPPost>>() {



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



Log.e("blog", " response "+ response.body());

mListPost = response.body();
progressBar.setVisibility(View.GONE);
for (int i=0; i<response.body().size();i++){
Log.e("main ", " title "+ response.body().get(i).getTitle().getRendered() + " "+
response.body().get(i).getId());

Log.e("main ", " HREF "+ response.body().get(i).getLinks().getWpFeaturedmedia().get(0).getHref() + " "+
response.body().get(i).getId());

Log.e("main ", " Date "+ convertDateToTimeAgo(response.body().get(i).getDate()) + response.body().get(i).getDate().getClass().getSimpleName());

Log.e("Blog ","Category id or Name - " + response.body().get(i).getLink());



String tempdetails = response.body().get(i).getExcerpt().getRendered().toString();
tempdetails = tempdetails.replace("<p>","");
tempdetails = tempdetails.replace("</p>","");
tempdetails = tempdetails.replace("[&hellip;]","");


list.add( new Model( Model.IMAGE_TYPE, response.body().get(i).getTitle().getRendered(),
tempdetails,
response.body().get(i).getLinks().getWpFeaturedmedia().get(0).getHref(),convertDateToTimeAgo(response.body().get(i).getDateGmt())));




}

adapter.notifyDataSetChanged();

}

@Override
public void onFailure(Call<List<WPPost>> call, Throwable t) {
Log.e("hellooo", "inside onfail t = "+ t);

}
});

}



public static List<WPPost> getList(){
return mListPost;
}



//convert date
public String convertDateToTimeAgo(String time) {
String inputPattern = "yyyy-MM-dd'T'HH:mm:ss";
String outputPattern = "yyyy-MM-dd HH:mm:ss'Z'";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);


inputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));


Date date = null;
String str = null;

try {


date = inputFormat.parse(time);
str = outputFormat.format(date);


long dateInMilliSecond = System.currentTimeMillis() - date.getTime();


str = toDuration(dateInMilliSecond);


Log.e("Blog - ", "Date " + date);
Log.e("Blog - ", "Str " + str);
// Log.e("Blog - ", "dateInMilliSecond " + dateInMillinSecond);

} catch (ParseException e) {
e.printStackTrace();
}



return str;
}





public static final List<Long> times = Arrays.asList(
TimeUnit.DAYS.toMillis(365),
TimeUnit.DAYS.toMillis(30),
TimeUnit.DAYS.toMillis(1),
TimeUnit.HOURS.toMillis(1),
TimeUnit.MINUTES.toMillis(1),
TimeUnit.SECONDS.toMillis(1) );
public static final List<String> timesString = Arrays.asList("year","month","day","hour","minute","second");

public static String toDuration(long duration) {

StringBuffer res = new StringBuffer();
for(int i=0;i< times.size(); i++) {
Long current = times.get(i);
long temp = duration/current;
if(temp>0) {
res.append(temp).append(" ").append( timesString.get(i) ).append(temp != 1 ? "s" : "").append(" ago");
break;
}
}
if("".equals(res.toString()))
return "0 seconds ago";
else
return res.toString();
}





}

正如我提到的,大多数答案都建议删除“列表”,我也这样做了,但没有用。

谢谢,

最佳答案

使用this站点从 json 响应生成正确的响应类

关于java.lang.IllegalStateException : Expected BEGIN_ARRAY but was BEGIN_OBJECT Android error 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56607205/

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