1.首先获得url,传入URL类,利用URL的openconnection方法,获得URLConnection,去的输入流,进行操作,具体代码如下:

public class NetConnection{
    public NetConnection(final String url,final HttpMethod method,final SuccessCallback successCallback,
            final FailCallback failCallback,final String ... key){
        new AsyncTask<Void, Void, String>() {

            protected String doInBackground(Void... params) {
                StringBuffer parameter = new StringBuffer();
                for (int i = 0; i < key.length; i+=2) {
                    parameter.append(key[i]).append("=").append(key[i+1]).append("&");
                }
                try {
                    URLConnection uc = null;
                    switch(method){
                    case POST:
                        uc = new URL(url).openConnection();
                        uc.setDoInput(true);
                        uc.setDoOutput(true);
                        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(uc.getOutputStream(),
                                Config.CHARSET));
                        bw.write(parameter.toString());
                        bw.flush();
                        break;
                    default:
                        uc = new URL(url+"?"+parameter.toString()).openConnection();
                        break;
                    }
                    System.out.println("request url:"+uc.getURL());
                    System.out.println("request data:"+parameter);
                    BufferedReader bf = new BufferedReader(new InputStreamReader(uc.getInputStream(), Config.CHARSET));
                    String line = null;
                    StringBuffer sb = new StringBuffer();
                    while ((line = bf.readLine()) != null) {
                        sb.append(line);
                    }
                    System.out.println("result:"+sb);
                    return sb.toString();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            protected void onPostExecute(String result) {
                if (result != null) {
                    if (successCallback != null) {
                        successCallback.onSuccess(result);
                    }
                }else {
                    if (failCallback != null) {
                        failCallback.onFail();
                    }
                }
                super.onPostExecute(result);
            }
        }.execute();
    }
    public static interface SuccessCallback{
        void onSuccess(String result);
    }
    public static interface FailCallback{
        void onFail();
    }
}

里面封装了两个接口,SuccessCallback和FailCallback,分别用于实现这个类的时候重写,来执行取得数据和灭幼取得数据的情况。参数中用字符串包含了请求参数。

android解析网络json数据(1)的更多相关文章

  1. 模拟QQ侧滑控件 实现三种界面切换效果(知识点:回调机制,解析网络json数据,fragment用法等)。

    需要用到的lib包 :解析json  gson包,从网络地址解析json数据成String字符串的异步网络解析工具AsyncHttpClient等 下载地址:点击下载 Xlistview 下拉上拉第三 ...

  2. 解析网络json数据,模拟美团界面显示。

    <?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android=&q ...

  3. Android解析服务器Json数据实例

    Json数据信息如下: { "movies": [ { "movie": "Avengers", "year": 201 ...

  4. [Swift通天遁地]四、网络和线程-(5)解析网络请求数据:String(字符串)、Data(二进制数据)和JSON数据

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. android 复杂的json数据解析

    1.通过谷歌的Gson来进行解析: json数据:sTotalString = {"message":"success","result": ...

  6. Gson解析复杂Json数据

    背景                                                                   json是一种数据格式,便于数据传输.存储.交换. gson是 ...

  7. 解析天气预报JSON数据

    解析天气预报JSON数据 JSON字符串 constjson2 = '{' + #13#10 +'"error":0,' + #13#10 +'"status" ...

  8. mormot解析天气预报JSON数据

    mormot解析天气预报JSON数据 uses SynCommons; constjson2 = '{' + #13#10 +'"error":0,' + #13#10 +'&qu ...

  9. Android网络请求与数据解析,使用Gson和GsonFormat解析复杂Json数据

    版权声明:未经博主允许不得转载 一:简介 [达叔有道]软件技术人员,时代作者,从 Android 到全栈之路,我相信你也可以!阅读他的文章,会上瘾!You and me, we are family ...

随机推荐

  1. python打包工具distutils、setuptools分析

    在上一篇博文中总结了python中导入包,安装包一条完整的线路.其中有一个有意思的知识点,安装包的方式有很多种,模块和包管理中打包,发布,安装也是值得研究的内容. python中安装包的方式有很多种: ...

  2. 机器学习技法:11 Gradient Boosted Decision Tree

    Roadmap Adaptive Boosted Decision Tree Optimization View of AdaBoost Gradient Boosting Summary of Ag ...

  3. 生成和配置https证书

    最近在做小程序,调用后台接口需要https协议请求,小程序之所以这么要求,也是因为http协议是明文传播文件数据的,出于数据安全考虑,必须使用https协议. http想实现为https 就需要为配置 ...

  4. [POI 2007]ZAP-Queries

    Description Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency) ...

  5. LOJ #6119. 「2017 山东二轮集训 Day7」国王

    Description 在某个神奇的大陆上,有一个国家,这片大陆的所有城市间的道路网可以看做是一棵树,每个城市要么是工业城市,要么是农业城市,这个国家的人认为一条路径是 exciting 的,当且仅当 ...

  6. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  7. hdu 4609 (FFT求解三角形)

    Problem Description King OMeGa catched three men who had been streaking in the street. Looking as id ...

  8. Hdu 5595 GTW likes math

    题意: 问题描述 某一天,GTW听了数学特级教师金龙鱼的课之后,开始做数学<从自主招生到竞赛>.然而书里的题目太多了,GTW还有很多事情要忙(比如把妹),于是他把那些题目交给了你.每一道题 ...

  9. NOIWC颓废记

    NOIWC大概就干了3件事情:吃.睡.浪. 吃: 目测绍兴一中的饭比二中的好吃多了,每天都有挺多的肉菜,还有一些甜品,而且是自助,不错的,但是一个不好的是排队时间太长了,于是我这么珍惜时间急着回宿舍的 ...

  10. 智能指针之 auto_ptr

    C++的auto_ptr所做的事情,就是动态分配对象以及当对象不再需要时自动执行清理,该智能指针在C++11中已经被弃用,转而由unique_ptr替代, 那这次使用和实现,就具体讲一下auto_pt ...