- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想解析json
与 retrofit2
但我的代码不起作用
此网络服务是:(请检查网络服务): http://services.groupkt.com/country/search?text=
这是我的模型类:
public class Country {
@SerializedName("name")
@Expose
private String name;
@SerializedName("alpha2_code")
@Expose
private String alpha2Code;
@SerializedName("alpha3_code")
@Expose
private String alpha3Code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAlpha2Code() {
return alpha2Code;
}
public void setAlpha2Code(String alpha2Code) {
this.alpha2Code = alpha2Code;
}
public String getAlpha3Code() {
return alpha3Code;
}
public void setAlpha3Code(String alpha3Code) {
this.alpha3Code = alpha3Code;
}
}
还有:
public class CountryResponse {
@SerializedName("messages")
@Expose
private List<String> messages = null;
@SerializedName("result")
@Expose
private List<Country> countryList = null;
public List<String> getMessages() {
return messages;
}
public void setMessages(List<String> messages) {
this.messages = messages;
}
public List<Country> getCountryList() {
return countryList;
}
public void setCountryList(List<Country> countryList) {
this.countryList = countryList;
}
}
还有:
public class Example {
@SerializedName("RestResponse")
@Expose
private CountryResponse restResponse;
public CountryResponse getRestResponse() {
return restResponse;
}
public void setRestResponse(CountryResponse restResponse) {
this.restResponse = restResponse;
}
}
这是我的界面类:
public interface APIInterface {
@GET("search?text=")
Call<Example> getCountries();
}
我编写了 2 个类来返回改造对象:
public class APIClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit == null) {
retrofit = new Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create()).build();
}
return retrofit;
}
}
public class APIUtils {
public static final String BASE_URL =
"http://services.groupkt.com/country/";
public static APIInterface getSOService() {
return APIClient.getClient(BASE_URL).create(APIInterface.class);
}
}
我的适配器:
public class ResponseAdapter extends
RecyclerView.Adapter<ResponseAdapter.ViewHolder> {
private List<Example> mItems;
private Context mContext;
Example example;
CountryResponse countyResponse;
public ResponseAdapter(Context context, Example example) {
this.example = example;
mContext = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item_list, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Example example = mItems.get(position);
TextView textView = holder.name;
textView.setText(example.getRestResponse()
.getCountryList().get(position).getName());
TextView textView1 = holder.alpha;
textView1.setText(example.getRestResponse().
getCountryList().get(position).getAl
pha2Code());
TextView textView2 = holder.alpha2;
textView2.setText(example.getRestResponse().
getCountryList().get(position).getAl
pha3Code());
}
@Override
public int getItemCount() {
return mItems.size();
}
public void updateAnswers(CountryResponse countryRes) {
countyResponse = countryRes;
notifyDataSetChanged();
}
private Example getItem(int adapterPosition) {
return mItems.get(adapterPosition);
}
public interface PostItemListener {
void onPostClick(long id);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name, alpha, alpha2;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
alpha = (TextView) itemView.findViewById(R.id.alpha);
alpha2 = (TextView) itemView.findViewById(R.id.alpha2);
}
}
}
我的 Activity :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_response);
apiInterface= APIUtils.getSOService();
mRecyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);
mAdapter=new ResponseAdapter(this,new Example());
RecyclerView.LayoutManager layoutManager = new
LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setHasFixedSize(true);
loadAnswers();
}
private void loadAnswers() {
apiInterface.getCountries().enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example>
response) {
mAdapter.updateAnswers(response.body().getRestResponse());
}
@Override
public void onFailure(Call<Example> call, Throwable t) {
}
});
}
运行时出现错误,显示
'java.util.Iterator java.util.List.iterator()' on a null object reference
最佳答案
你有
@Override
public int getItemCount() {
return mItems.size();
}
public void updateAnswers(CountryResponse countryRes) {
countyResponse = countryRes;
notifyDataSetChanged();
}
您的列表一开始就没有初始化。其次,您需要用项目填充您的列表。并且您的列表必须是国家/地区列表,而不是示例类型
您需要更改为
public void updateAnswers(CountryResponse countryRes) {
mItems.addAll(countryRes.getCountryList())
notifyDataSetChanged();
}
并初始化列表
List<Country> mItems = new ArrayList();
// note its of type Country not of Example
最后在 onBindViewHolder
中相应地更新您的 View
textView.setText(mItems.get(position).getName());
textView1.setText(mItems.get(position).getAlpha2Code());
textView2.setText(mItems.get(position).getAlpha3Code());
关于java - 使用 Retrofit 解析 Android 的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47032349/
我正在尝试迁移我的应用程序以使用 RxJava。我已经在使用 Retrofit,因此我正在尝试使用方法返回 Observables 的 Retrofit 接口(interface)。但是我现在在针对它
我想 post 数据如下: { "user_id":"14545646", "list":["4545645","4545645","4545645","4545645"]
我是改造新手。我向网站发出 POST 请求。网站以 HTML 形式返回响应。所以我会解析它。但是 Retrofit 尝试将其解析为 JSON。怎么办? @FormUrlEncoded @POST("/
我想异步执行 2 个网络调用 - 我正在使用 Retrofit+RxJava 来完成这个。这个逻辑来自一个简单的 Runner 类来测试解决方案。注意:这主要涉及服务器端的 RxJava。 我的代码如
我最近在改造的存储库中使用 Flow。 Result 的密封类 enum class ApiStatus{ SUCCESS, ERROR, LOADING } sealed c
我目前正在开发 Retrofit2 客户端的错误处理部分(使用 Retrofit 的 Rx 实现)。 找了一段时间,没有找到简单的方法将Retrofit返回的ResponseBody反序列化到我报错的
我从 retrofit 更新到 retrofit2 后出现此错误。 FATAL EXCEPTION: OkHttp Dispatcher Process: nz.co.datacom.mars.jun
在使用 RxJava 和 Retrofit 2 时,我正在尝试创建单元测试来覆盖我的应用何时收到特定响应。 我遇到的问题是,在 Retrofit 2 中,我看不到在不使用反射的情况下创建 retrof
在 Retrofit 1.9.x 中有一个 RetrofitError.Kind.NETWORK这让您可以轻松确定故障是否由网络错误引起。在新的 2.0 API 中,我们不再有 RetrofitErr
有没有办法用Refit动态输入参数? 我的 Refit 界面中有这段代码: [Get("/click?{parm}")] Task> SaveClick(string parm
有没有办法用Refit动态输入参数? 我的 Refit 界面中有这段代码: [Get("/click?{parm}")] Task> SaveClick(string parm
我知道,Retrofit 在内部使用 OkHttp。但是,我可以看到一些开发人员提供了以下方法的应用 return new Retrofit.Builder() .baseUrl(Bu
在项目上安装这个库之后: compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.0
在 Retrofit 1.x 中,我使用以下模式创建 API 服务类,该类模拟某些构建变体的不良网络连接。 // Retrofit 1 private T create(Class apiServi
Retrofit请求API格式(Android): @POST("getOrderStatus") @Headers("Content-Type:application/json") Obser
当我实例化 RestAdapter 时,我的应用程序总是崩溃 private void submitForm(SignupForm form){ RestAdapter adapter = n
我正要从 retrofit 1.9 迁移到最新版本并遇到问题... 我的设置: 三星 Galaxy S7 Edge(Android 7) 改造 2.3 OkHttp 3.8 迁移后突然遇到这个问题:
我正在尝试在 2.5.1-SNAPSHOT 中使用 retrofit 的协程支持,但我不断收到一个奇怪的异常。 我的改造服务类有: @GET("weather") suspend fun getFor
我正在尝试 Ktor通过转换一些当前正在使用的现有项目 Retrofit . 虽然我可以很容易地将请求转换为: client.get { url("$BASE_URL/something/so
使用改造 2,如何为上传的文件设置动态名称? 目前是这样的: @Part("avatar\"; filename=\"image\" ") RequestBody image, 但是,上传的文件名将是
我是一名优秀的程序员,十分优秀!