如何将截击json解析外部化为一个单独的类文件,从而使代码松散耦合



我有一个名为Client.class的类。在这个类中,我得到了一个方法,如下所示,调用一个截击来激发api请求。

 public void dispatchLoginRequest() {
        //        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        //        Log.d(TAG, "ADS " + frameLoginJson().toString());
        pDialog.setMessage("Logging in ...");
        showDialog();
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, "http://52.11.242.90:8082/api/login", frameLoginJson(), new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                parseLoginResponse(response);
//                aparjithaDb.parseLoginResponse(response, this);
                Parser.getInstance().parseLoginResponse(response,this,getApplicationContext());
                pDialog.hide();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, "log" + frameLoginJson().toString());
                error.printStackTrace();
                pDialog.hide();
                Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        AppController.getInstance().getRequestQueue().add(jsonArrayRequest);
    }

我在另一个名为Parse.class的类中有以下方法,我在其中解析响应并将其添加到数据库中。

public void parseLoginResponse(JSONArray response, ClientLogin clientLogin, ContextWrapper contextWrapper) {
        for (int i = 0; i < response.length(); i++) {
            try {
                JSONObject value = response.getJSONObject(i);
                JSONArray configuration = value.getJSONArray("configuration");
                int group_id = value.getInt("group_id");
                String group_name = value.getString("group_name");
                JSONObject menu = value.getJSONObject("menu");
                JSONObject menus = menu.getJSONObject("menus");
                JSONArray master = menus.getJSONArray("Master");
                JSONArray transaction = menus.getJSONArray("Transaction");
                JSONArray report = menus.getJSONArray("Report");
                boolean approvalExists = isApprovalExists(transaction, "Compliance Approval");
                boolean taskExists = isTaskExists(transaction, "Compliance Task Details");
                Toast.makeText(contextWrapper.getApplicationContext(), "COOL" + approvalExists, Toast.LENGTH_SHORT).show();
                for (int j = 0; j < configuration.length(); j++) {
                    JSONObject jsonobject = configuration.getJSONObject(i);
                    int period_from = jsonobject.getInt("period_from");
                    int period_to = jsonobject.getInt("period_to");
                    int country_id = jsonobject.getInt("country_id");
                    int domain_id = jsonobject.getInt("domain_id");
                    Db.getInstance(contextWrapper).addClientConfig(new ClientConfig(j, String.valueOf(period_from), String.valueOf(country_id), String.valueOf(period_to), String.valueOf(domain_id), group_name, String.valueOf(group_id)));
                }
                Intent intent = new Intent(clientLogin, Client.class);
                intent.putExtra("approvalExists", "" + approvalExists);
                intent.putExtra("taskExists", "" + taskExists);
                clientLogin.startActivity(intent);
                // logClientDb();

            } catch (Exception e) {
                Toast.makeText(contextWrapper.getApplicationContext(), "Please Check your user credentials..." + e.toString(), Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }
    }

问题是我得到以下错误

Error:(91, 37) error: method parseLoginResponse in class Parser cannot be applied to given types; required: JSONArray,ClientLogin,ContextWrapper found: JSONArray,<anonymous Listener<JSONArray>>,Context reason: actual argument <anonymous Listener<JSONArray>> cannot be converted to ClientLogin by method invocation conversion

我只是试图将解析json并在使用截击时将其添加到db的方式外部化。我该怎么做?

对于请求,请按如下方式修改-JsonArrayRequest请求=新JsonArray请求(url,requestSuccessListener(),requestErrorListener(());

现在编写u r public void外部函数requestSuccessListener和requestErrorListener。它会起作用的。

最新更新