com.bruce.myrecorder.netwrapper.BaseNetWrapper.java Source code

Java tutorial

Introduction

Here is the source code for com.bruce.myrecorder.netwrapper.BaseNetWrapper.java

Source

package com.bruce.myrecorder.netwrapper;

import android.util.Log;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.bruce.myrecorder.utils.CommonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Copyright 2014 Bruce Wu.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class BaseNetWrapper {
    protected String url;
    protected JsonObjectRequest jsonObjectRequest;
    protected Map<String, String> parameters;
    protected DoResponse doResponse;
    protected String responseMessage;
    protected Boolean resultState;

    public BaseNetWrapper() {
        parameters = new HashMap<String, String>();
    }

    public JsonObjectRequest getJsonRequst() {
        jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                CommonUtils.showLog(response.toString());
                try {
                    if (response.getInt("flag") == 101) {
                        resultState = true;
                    } else {
                        resultState = false;
                    }
                    responseMessage = response.getString("message");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                doResponse.successResponse(responseMessage, resultState);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
                doResponse.errorResponse();
            }
        });

        return jsonObjectRequest;
    }

    public String getResponseMessage() {
        return responseMessage;
    }

    public Boolean getResultState() {
        return resultState;
    }

    public interface DoResponse {
        public void successResponse(String responseMessage, Boolean resultState);

        public void errorResponse();
    }
}