优秀案例

Android开源快递查询应用——小马快递

2016-01-20  本文已影响1604人  王晨彦

小马快递

前言

这是我第一个独立完成的项目,时隔多年又把它拿出来重构了一下代码。

简介

小马快递,您的好帮手。查询并跟踪快递,快递信息及时掌握。

支持全国100多家快递公司,支持扫码查询,智能识别快递公司。

附带生成二维码小工具,方便实用。体积小巧,无广告,无多余权限。

更新说明

v 2.0

v 1.5

v 1.4

下载地址

fir:https://fir.im/ponyexpress

项目

公开API

开源技术

关键代码

网络请求Volley + Gson

public static void query(String type, String postId, final HttpCallback<SearchResult> callback) {
    String action = "/query";
    Map<String, String> params = new HashMap<>(2);
    params.put("type", type);
    params.put("postid", postId);
    String url = makeUrl(action, params);
    GsonRequest<SearchResult> request = new GsonRequest<SearchResult>(url, SearchResult.class,
            new Response.Listener<SearchResult>() {
                @Override
                public void onResponse(SearchResult searchResult) {
                    callback.onResponse(searchResult);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    callback.onError(volleyError);
                }
            }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            headers.put(HEADER_REFERER, BASE_URL);
            return headers;
        }
    };
    request.setShouldCache(false);
    getRequestQueue().add(request);
}

封装GsonRequest

public class GsonRequest<T> extends Request<T> {
    private Class<T> mClass;
    private Response.Listener<T> mListener;
    private Gson mGson;

    public GsonRequest(int method, String url, Class<T> clazz, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        mClass = clazz;
        mListener = listener;
        mGson = new Gson();
    }

    public GsonRequest(String url, Class<T> clazz, Response.Listener<T> listener, Response.ErrorListener errorListener) {
        this(Method.GET, url, clazz, listener, errorListener);
    }

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        String jsonString;
        try {
            jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(mGson.fromJson(jsonString, mClass), HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Response.error(new ParseError(e));
        }
    }

    @Override
    protected void deliverResponse(T response) {
        mListener.onResponse(response);
    }
}

截图





关于作者

简书:http://www.jianshu.com/users/3231579893ac

微博:http://weibo.com/wangchenyan1993

License

Copyright 2016 wangchenyan

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

   http://www.apache.org/licenses/LICENSE-2.0

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.
上一篇下一篇

猜你喜欢

热点阅读