Search in sources :

Example 6 with VolleyError

use of com.android.volley.VolleyError in project MVCHelper by LuckyJayce.

the class LoginAsyncTask method execute.

@Override
public RequestHandle execute(final ResponseSender<User> sender) throws Exception {
    String url = "https://www.baidu.com";
    Uri.Builder builder = Uri.parse(url).buildUpon();
    builder.appendQueryParameter("userName", name);
    builder.appendQueryParameter("password", password);
    StringRequest jsonObjRequest = new StringRequest(Request.Method.GET, builder.toString(), new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            if (TextUtils.isEmpty(name)) {
                sender.sendError(new BizException("请输入用户名"));
            } else if (TextUtils.isEmpty(password)) {
                sender.sendError(new BizException("请输入密码"));
            } else if (name.equals("LuckyJayce") && password.equals("111")) {
                sender.sendData(new User("1", "LuckyJayce", 23, "中国人"));
            } else {
                sender.sendError(new BizException("用户名或者密码不正确"));
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            sender.sendError(error);
        }
    });
    MyVolley.getRequestQueue().add(jsonObjRequest);
    return new VolleyRequestHandle(jsonObjRequest);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) User(com.shizhefei.test.models.enties.User) VolleyRequestHandle(com.shizhefei.test.models.datasource.volley.VolleyRequestHandle) StringRequest(com.android.volley.toolbox.StringRequest) BizException(com.shizhefei.test.models.exception.BizException) Uri(android.net.Uri)

Example 7 with VolleyError

use of com.android.volley.VolleyError in project FastDev4Android by jiangqqlmj.

the class VolleyTestActivity method backLinearClick.

@Click({ R.id.top_bar_linear_back, R.id.btn_string, R.id.btn_json, R.id.btn_image_request, R.id.btn_image_loader, R.id.btn_image_network, R.id.btn_string_post, R.id.btn_loader_list, R.id.btn_gson, R.id.btn_fdv_get_params, R.id.btn_fdv_post_params })
public void backLinearClick(View view) {
    switch(view.getId()) {
        case R.id.top_bar_linear_back:
            this.finish();
            break;
        case R.id.btn_string:
            //获取字符串
            Log.d(TAG, "点击获取字符串...");
            new Fdv_StringRequest(VolleyTestActivity.this).get("http://www.baidu.com", new Fdv_CallBackListener<String>() {

                @Override
                public void onSuccessResponse(String response) {
                    tv_result.setVisibility(View.VISIBLE);
                    img_result.setVisibility(View.GONE);
                    img_result_network.setVisibility(View.GONE);
                    tv_result.setText(response.toString());
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
            break;
        case R.id.btn_json:
            //获取json
            Log.d(TAG, "点击获取json...");
            new Fdv_JsonObjectRequest(VolleyTestActivity.this).get("http://interface.zttmall.com/update/mallUpdate", new Fdv_CallBackListener<JSONObject>() {

                @Override
                public void onSuccessResponse(JSONObject response) {
                    Gson gson = new Gson();
                    tv_result.setVisibility(View.VISIBLE);
                    img_result.setVisibility(View.GONE);
                    img_result_network.setVisibility(View.GONE);
                    tv_result.setText(gson.fromJson(response.toString(), UpdateBean.class).toString());
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
            break;
        case R.id.btn_image_request:
            //获取图片
            //http:\/\/interface.zttmall.com\/Images\/upload\/image\/20150325\/20150325083110_0898.jpg
            Log.d(TAG, "点击获取图片...");
            ImageRequest imageRequest = new ImageRequest("http://interface.zttmall.com/Images/upload/image/20150325/20150325083110_0898.jpg", new Response.Listener<Bitmap>() {

                @Override
                public void onResponse(Bitmap response) {
                    tv_result.setVisibility(View.GONE);
                    img_result.setVisibility(View.VISIBLE);
                    img_result.setImageBitmap(response);
                    img_result_network.setVisibility(View.GONE);
                }
            }, 0, 0, ImageView.ScaleType.FIT_XY, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
            requestQueue.add(imageRequest);
            break;
        case R.id.btn_image_loader:
            //使用imageloader进行获取图片
            ImageLoader imageLoader = new ImageLoader(requestQueue, new Fdv_ImageCache());
            tv_result.setVisibility(View.GONE);
            img_result.setVisibility(View.VISIBLE);
            img_result_network.setVisibility(View.GONE);
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(img_result, R.drawable.ic_loading, R.drawable.ic_loading);
            imageLoader.get("http://interface.zttmall.com//Images//upload//image//20150328//20150328105404_2392.jpg", listener);
            break;
        case R.id.btn_image_network:
            //采用NetworkImageView imageview控件
            ImageLoader network_imageLoader = new ImageLoader(requestQueue, new Fdv_ImageCache());
            img_result.setVisibility(View.GONE);
            tv_result.setVisibility(View.GONE);
            img_result_network.setVisibility(View.VISIBLE);
            img_result_network.setImageUrl("http://interface.zttmall.com//Images//upload//image//20150325//20150325083214_8280.jpg", network_imageLoader);
            break;
        case R.id.btn_string_post:
            //修改Volley源代码,扩展StringRequest支持post参数设置
            final Map<String, String> params = new HashMap<String, String>();
            params.put("username", "张三");
            params.put("password", "12345");
            StringRequest post_stringRequest = new StringRequest(Request.Method.POST, "http://10.18.3.123:8080/SalesWebTest/TestVolleyPost", new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    tv_result.setVisibility(View.VISIBLE);
                    img_result.setVisibility(View.GONE);
                    img_result_network.setVisibility(View.GONE);
                    tv_result.setText(response.toString());
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }) {

                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    return params;
                }
            };
            requestQueue.add(post_stringRequest);
            break;
        case R.id.btn_loader_list:
            //进行使用ImageLoader加载图片列表
            openActivity(VolleyLoaderActivity_.class);
            break;
        case R.id.btn_gson:
            //使用扩展工具 GsonRequest进行请求
            GsonRequest<UpdateBean> gsonRequest = new GsonRequest<UpdateBean>("http://interface.zttmall.com/update/mallUpdate", new Response.Listener<UpdateBean>() {

                @Override
                public void onResponse(UpdateBean response) {
                    tv_result.setVisibility(View.VISIBLE);
                    img_result.setVisibility(View.GONE);
                    img_result_network.setVisibility(View.GONE);
                    tv_result.setText(response.toString());
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }, UpdateBean.class);
            requestQueue.add(gsonRequest);
            break;
        case R.id.btn_fdv_get_params:
            //get请求  传入请求参数
            Map<String, String> params_get = new HashMap<String, String>();
            params_get.put("username", "张三");
            params_get.put("password", "12345");
            new Fdv_StringRequest(this).get("http://10.18.3.123:8080/SalesWebTest/TestVolleyPost", new Fdv_CallBackListener<String>() {

                @Override
                public void onSuccessResponse(String response) {
                    tv_result.setVisibility(View.VISIBLE);
                    img_result.setVisibility(View.GONE);
                    img_result_network.setVisibility(View.GONE);
                    tv_result.setText(response.toString());
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }, params_get);
            break;
        case R.id.btn_fdv_post_params:
            //post请求  传入请求参数
            Map<String, String> params_post = new HashMap<String, String>();
            params_post.put("username", "张三");
            params_post.put("password", "12345");
            new Fdv_StringRequest(this).post("http://10.18.3.123:8080/SalesWebTest/TestVolleyPost", new Fdv_CallBackListener<String>() {

                @Override
                public void onSuccessResponse(String response) {
                    tv_result.setVisibility(View.VISIBLE);
                    img_result.setVisibility(View.GONE);
                    img_result_network.setVisibility(View.GONE);
                    tv_result.setText(response.toString());
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }, params_post);
            break;
    }
}
Also used : GsonRequest(com.android.volley.toolbox.GsonRequest) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) Bitmap(android.graphics.Bitmap) ImageRequest(com.android.volley.toolbox.ImageRequest) Fdv_ImageCache(com.chinaztt.fdv.Fdv_ImageCache) UpdateBean(com.chinaztt.fda.entity.UpdateBean) Fdv_StringRequest(com.chinaztt.fdv.Fdv_StringRequest) VolleyError(com.android.volley.VolleyError) StringRequest(com.android.volley.toolbox.StringRequest) Fdv_StringRequest(com.chinaztt.fdv.Fdv_StringRequest) Fdv_JsonObjectRequest(com.chinaztt.fdv.Fdv_JsonObjectRequest) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) ImageLoader(com.android.volley.toolbox.ImageLoader) Click(org.androidannotations.annotations.Click)

Example 8 with VolleyError

use of com.android.volley.VolleyError in project FastDev4Android by jiangqqlmj.

the class NetworkImageView method loadImageIfNecessary.

/**
     * Loads the image for the view if it isn't already loaded.
     * @param isInLayoutPass True if this was invoked from a layout pass, false otherwise.
     */
void loadImageIfNecessary(final boolean isInLayoutPass) {
    int width = getWidth();
    int height = getHeight();
    ScaleType scaleType = getScaleType();
    boolean wrapWidth = false, wrapHeight = false;
    if (getLayoutParams() != null) {
        wrapWidth = getLayoutParams().width == LayoutParams.WRAP_CONTENT;
        wrapHeight = getLayoutParams().height == LayoutParams.WRAP_CONTENT;
    }
    // if the view's bounds aren't known yet, and this is not a wrap-content/wrap-content
    // view, hold off on loading the image.
    boolean isFullyWrapContent = wrapWidth && wrapHeight;
    if (width == 0 && height == 0 && !isFullyWrapContent) {
        return;
    }
    // currently loaded image.
    if (TextUtils.isEmpty(mUrl)) {
        if (mImageContainer != null) {
            mImageContainer.cancelRequest();
            mImageContainer = null;
        }
        setDefaultImageOrNull();
        return;
    }
    // if there was an old request in this view, check if it needs to be canceled.
    if (mImageContainer != null && mImageContainer.getRequestUrl() != null) {
        if (mImageContainer.getRequestUrl().equals(mUrl)) {
            // if the request is from the same URL, return.
            return;
        } else {
            // if there is a pre-existing request, cancel it if it's fetching a different URL.
            mImageContainer.cancelRequest();
            setDefaultImageOrNull();
        }
    }
    // Calculate the max image width / height to use while ignoring WRAP_CONTENT dimens.
    int maxWidth = wrapWidth ? 0 : width;
    int maxHeight = wrapHeight ? 0 : height;
    // The pre-existing content of this view didn't match the current URL. Load the new image
    // from the network.
    ImageContainer newContainer = mImageLoader.get(mUrl, new ImageListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (mErrorImageId != 0) {
                setImageResource(mErrorImageId);
            }
        }

        @Override
        public void onResponse(final ImageContainer response, boolean isImmediate) {
            // the main thread.
            if (isImmediate && isInLayoutPass) {
                post(new Runnable() {

                    @Override
                    public void run() {
                        onResponse(response, false);
                    }
                });
                return;
            }
            if (response.getBitmap() != null) {
                setImageBitmap(response.getBitmap());
            } else if (mDefaultImageId != 0) {
                setImageResource(mDefaultImageId);
            }
        }
    }, maxWidth, maxHeight, scaleType);
    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
Also used : VolleyError(com.android.volley.VolleyError) ImageListener(com.android.volley.toolbox.ImageLoader.ImageListener) ImageContainer(com.android.volley.toolbox.ImageLoader.ImageContainer)

Example 9 with VolleyError

use of com.android.volley.VolleyError in project FastDev4Android by jiangqqlmj.

the class Fdv_JsonArrayRequest method post.

/**
     * POST请求返回JSONArray对象
     * @param url   请求地址
     * @param listener   请求数据返回回调接口
     * @param params   POST请求参数
     */
public void post(String url, final Fdv_CallBackListener<JSONArray> listener, Map<String, String> params) {
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, url, null, new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {
            if (listener != null) {
                listener.onSuccessResponse(response);
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (listener != null) {
                listener.onErrorResponse(error);
            }
        }
    });
    addRequest(jsonArrayRequest, params);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JsonArrayRequest(com.android.volley.toolbox.JsonArrayRequest) JSONArray(org.json.JSONArray)

Example 10 with VolleyError

use of com.android.volley.VolleyError in project FastDev4Android by jiangqqlmj.

the class Fdv_JsonObjectRequest method get.

/**
     * 请求返回JSONObject对象 Get请求 无参数,或者get请求的参数直接拼接在URL上面
     * @param url   请求地址
     * @param listener  数据回调接口
     */
public void get(String url, final Fdv_CallBackListener<JSONObject> listener) {
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (listener != null) {
                listener.onSuccessResponse(response);
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (listener != null) {
                listener.onErrorResponse(error);
            }
        }
    });
    addRequest(jsonObjectRequest);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Aggregations

VolleyError (com.android.volley.VolleyError)180 Response (com.android.volley.Response)121 JSONObject (org.json.JSONObject)104 HashMap (java.util.HashMap)70 JSONException (org.json.JSONException)66 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)61 RequestQueue (com.android.volley.RequestQueue)57 User (model.User)49 JSONArray (org.json.JSONArray)35 CustomRequestArray (dz.easy.androidclient.Util.CustomRequestArray)18 Context (android.content.Context)16 Toast (android.widget.Toast)16 StringRequest (com.android.volley.toolbox.StringRequest)16 RestRequest (com.wordpress.rest.RestRequest)16 CustomRequest (dz.easy.androidclient.Util.CustomRequest)12 TextView (android.widget.TextView)11 MockHttpStack (com.android.volley.mock.MockHttpStack)9 GsonRequest (com.android.volley.toolbox.GsonRequest)9 ImageContainer (com.android.volley.toolbox.ImageLoader.ImageContainer)9 Test (org.junit.Test)9