Android 项目(详解二)—— 网络连接(2)
2015-10-12 15:38
661 查看
前面我们介绍了简单的InternetConnect类,和包含所有连接的MyAllHttpMethod类,在这个类中有所有的网络连接包括登录、注册、提交数据给服务器等一系列的网络连接操作。
现在我们在MyAllHttpMethod类中添加一个提交数据,获得服务器数据的方法。
具体来说,就是调用这个方法,可以连接服务器,提交type类型(其实也应该提交的数据时json,但这里没有提交具体数据),服务器通过type返回给客户端json数据(这里返回的是某一时间的客户总数)。
我们再来回顾一下网络连接的具体过程吧^^
添加或者更改的几个包的状态:
我们可以看到这个提交报表的方法requestChart方法提交的参数并没有封装成json数据,而真正项目中要封装成json数据,然后服务器从数据库获得数据也封装成json数据,再返回给客户端。
这是上面代码中的提交方法:
1)这里可以看到我们使用了gson来解析json数据,因为它在解析json中有很多array时是非常方便的,但使用的前提要要导入gson包。
2)获得数据时需要一个保存数据的类NewSales类:
3)为了方便我们添加了两个工具类:
GsonUtils类:(gson将json解析到list中)
ToastUtils类:(方便使用toast弹窗)
现在我们在MyAllHttpMethod类中添加一个提交数据,获得服务器数据的方法。
具体来说,就是调用这个方法,可以连接服务器,提交type类型(其实也应该提交的数据时json,但这里没有提交具体数据),服务器通过type返回给客户端json数据(这里返回的是某一时间的客户总数)。
我们再来回顾一下网络连接的具体过程吧^^
添加或者更改的几个包的状态:
网络连接内部的更改:
1.权限设置.
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
2.MyAllHttpMethod类中的提交方法
package com.example.administrator.redcircle.Internet; import com.example.administrator.redcircle.ToolsClass.NetWorkConfig; import java.util.HashMap; /** * Created by 董梦娇 on 2015/10/10. * 这里写所有的连接,包括登录注册等 */ public class MyAllHttpMethod { //单例 private MyAllHttpMethod(){} private static MyAllHttpMethod myAllHttpMethod; public synchronized static MyAllHttpMethod newInstance(){ if (myAllHttpMethod==null){ myAllHttpMethod=new MyAllHttpMethod(); } return myAllHttpMethod; } public static void login(HashMap<String,String> params,InternetConnect.onConnectionListener listener){ InternetConnect.newInstance().addReqest(NetWorkConfig.URL_LOGIN,params,listener); } public static void register(){ } /** * 请求报表数据 * @param params:包含手机imei码,用户名username,当前时间time * @param listener 网络连接的回调方法 */ public void requestChart(HashMap<String,String> params,InternetConnect.onConnectionListener listener){ //通常这里都是将params转成json(包含type类型,date新增,时间time),再请求服务器,然后服务器判断类型来返回相应的数据。 InternetConnect.newInstance().addReqest(NetWorkConfig.URL_CHART,params,listener); } }
我们可以看到这个提交报表的方法requestChart方法提交的参数并没有封装成json数据,而真正项目中要封装成json数据,然后服务器从数据库获得数据也封装成json数据,再返回给客户端。
这是上面代码中的提交方法:
3.NetWorkConfig类中添加了一个提交数据的服务器的url
/** * Created by 董梦娇 on 2015/10/10. */ public class NetWorkConfig { // public static final String URL_LOGIN = "http://www.baidu.com"; public static final String URL_LOGIN = "http://www.baidu.com"; public static final String URL_CHART = "http://192.168.0.74:8080/myServer/Myserverlet"; }
4.在主活动中使用时:
点击事件中:HashMap<String,String> params = new HashMap<String, String>(); MyAllHttpMethod.newInstance().requestChart(params, new InternetConnect.onConnectionListener() { @Override public void onNullInternet() { ToastUtils.showToast("没有网络,请打开网络!"); } @Override public void onFailConnection(int errorCode) { ToastUtils.showToast("网络连接错误"+errorCode); } @Override public void onSuccess(String response) { //成功后。将返回的json数据进行解析 //可以通过所写的gsonutil工具解析json try { JSONObject object = new JSONObject(response);//建立response的jsonobject JSONArray array = object.getJSONArray("data"); //解析好json并存到list中 List<NewSales> list= GsonUtils.readJsonArray(array,NewSales.class); //将获得的数据存到本地数据库中 // DbUtils.create(getApplication(),"scale").saveAll(list); //查看是否获得了数据 for (NewSales sales:list){ Log.d("scales","时间"+sales.getTime()+"数量"+sales.getNum()); } } catch (JSONException e) { e.printStackTrace(); } //弹出toast ToastUtils.showToast("网络连接成功"+response); } });
1)这里可以看到我们使用了gson来解析json数据,因为它在解析json中有很多array时是非常方便的,但使用的前提要要导入gson包。
2)获得数据时需要一个保存数据的类NewSales类:
public class NewSales { String time; String num; public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } }
3)为了方便我们添加了两个工具类:
GsonUtils类:(gson将json解析到list中)
package com.example.administrator.redcircle.ToolsClass; import com.google.gson.Gson; import org.json.JSONArray; import org.json.JSONException; import java.util.ArrayList; import java.util.List; /** * Created by 董梦娇 on 2015/10/12. */ public class GsonUtils { public static <T> List<T> readJsonArray(JSONArray array,Class<T> entityType){ Gson gson = new Gson(); List<T> list = new ArrayList<>(); for (int i = 0; i <array.length();i++){//通过gson将解析json try { T t = gson.fromJson(array.getJSONObject(i).toString(),entityType); list.add(t); } catch (JSONException e) { e.printStackTrace(); } } return list; } }
ToastUtils类:(方便使用toast弹窗)
public class ToastUtils { public static void showToast(String str){ Toast.makeText(AppApplication.getApplication(),str,Toast.LENGTH_LONG).show(); } }
服务器中的返回方法:
我们并不做后台,大体看一下相关文章推荐
- 保存一下,回去再看,关于TCP连接的分析的一篇文章
- Android 项目实践(二)——网络连接
- TCP/IP TIME_WAIT状态原理(转)
- HttpURLConnection详解
- 计算机网络(三)HTTP协议相关基础
- iOS开发网络数据之AFNetworking使用
- Cocos发育Visual Studio下一个HttpClient开发环境设置
- 使用nginx当做http的负载均衡
- IOS 9 同时兼容http以及https请求
- HDU 4280 Island Transport 网络流
- TCP/IP协议三次握手与四次握手流程解析
- HttpClient Cookies设置
- 基于netty TCP server server主动给client 发消息解决方案
- Android 项目(一):网络请求封装(一)
- 【Android-007】【网络图片查看器】
- IOS开发之ASIHTTPRequest下载示例
- JSF中获得HTTP SESSION和Request
- 黑马程序员—网络编程TCP与UDP简介
- Xcode7 http请求出错
- Android中应对封装好的数据库jar包请求Http数据的结果回调