如何创建排球服务器响应类

时间:2019-07-11 07:40:34

标签: android android-volley android-json

StringRequest stringRequest =新的StringRequest(Request.Method.GET,URL,新的Response.Listener(){@Override public void onResponse(String response){Log.e(“ onResponseSuccess”,response.toString()+“ check “);

公共类VollyResponse {

private Context mContext;
private BaseActivity baseActivity;

public VollyResponse(Context context) {
    this.mContext = context;
    baseActivity = (BaseActivity) context;
}

public void sendRequest(String body, final GetVollyResponse getVollyResponse) {
    Log.e("URL", body);
    // baseActivity.appDialogs.showAlertDialog();

    //baseActivity.appDialogs.showMaterialProgress(mContext);

    StringRequest stringRequest = new StringRequest(body, new Response.Listener<String>() {
        @Override public void onResponse(String response) {

            //  baseActivity.appDialogs.hideMaterialProgress();

            try {
                JSONObject result = new JSONObject(response);

                Log.e("Response", result + "");

                if (result.getString("status").equalsIgnoreCase("true")) {
                    getVollyResponse.onSuccess(result);
                } else {

                    getVollyResponse.onFailure(result.getString("message"));
                }
            }
            catch(JSONException e) {
                e.printStackTrace();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override public void onErrorResponse(VolleyError error) {
            try {
                getVollyResponse.onVollyFailure(error);
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    });

    com.android.volley.RequestQueue requestQueue = Volley.newRequestQueue(mContext);
    requestQueue.add(stringRequest);
}

}

公共接口GetVollyResponse {

public void onSuccess(JSONObject result) throws Exception;
public void onFailure(String message) throws Exception;
public void onVollyFailure(VolleyError error) throws Exception;

}

0 个答案:

没有答案