您的位置:首页 > 理论基础 > 计算机网络

Android 项目(详解二)—— 网络连接(2)

2015-10-12 15:38 661 查看
前面我们介绍了简单的InternetConnect类,和包含所有连接的MyAllHttpMethod类,在这个类中有所有的网络连接包括登录、注册、提交数据给服务器等一系列的网络连接操作。

现在我们在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();

}
}


服务器中的返回方法:

我们并不做后台,大体看一下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: