相关疑难解决方法(0)

创建适配器映像时无法应用对象中的object()

我正在创建一个适配器映像,并且出现以下2个错误:

这是代码

public class GridViewAdapter {

    private Context mcontext;
    private int layoutResourceId;

    public GridViewAdapter(Context context, int layoutResourceId, ArrayList<Listitem> listitem) {
        super(context, layoutResourceId, listitem);
        this.layoutResourceId = layoutResourceId;
        this.mcontext =context;

    }
Run Code Online (Sandbox Code Playgroud)

这是第二个错误

cannot resolve method getitem()

   ` Listitem item = getItem(position);`

if (row == null) {
            LayoutInflater inflater = LayoutInflater.from(mcontext);
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.imageTitle = (TextView) row.findViewById(R.id.text);
            holder.imageView = (ImageView) row.findViewById(R.id.imageView);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }
        Listitem item = getItem(position);
Run Code Online (Sandbox Code Playgroud)

android

3
推荐指数
1
解决办法
3810
查看次数

改造response.errorBody()为null

我正在做一些REST调用,并获得响应.有时,响应不会正常,所以我需要解析响应,并以适当的方式呈现它以供用户注意并做出反应.我有这个代码:

if(response.code()!=200){
      JSONArray jsonError=null;
      try {
         Log.i("David", "Incorrect response: "+response.errorBody().string());

         jsonError = new JSONArray(response.errorBody().string());//After this line, jsonError is null
         JSONObject message=jsonError.getJSONObject(0);

         String errorJson=message.getString("message");
         Log.i("David", "Error received: "+errorJson);
         AlertDialog.Builder dialog=new AlertDialog.Builder(getActivity());
         dialog.setTitle(getString(R.string.error));
         dialog.setMessage(getString(R.string.errorfound) + errorJson);
         dialog.setPositiveButton(getString(R.string.aceptar), new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
              }
                  });
                            dialog.show();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
Run Code Online (Sandbox Code Playgroud)

这样,日志行"错误响应"显示完美,包含错误代码,消息等.这是收到的JSON:

[{"coderror":"-2","gravedad":"1","message":"Device imei 34543564768463435345 not logged in","cause":""}]
Run Code Online (Sandbox Code Playgroud)

我的问题是,正如我所说,日志记录工作正常,但是当我尝试将JSON转换为JsonArray时......它失败了.在下一行中,JsonError对象变为null.

为什么我不能解析回复?谢谢.

android json retrofit okhttp

2
推荐指数
1
解决办法
2028
查看次数

标签 统计

android ×2

json ×1

okhttp ×1

retrofit ×1