从页面发出请求,到返回数据的整个过程
MyAllHttpMethod
package com.redcircle.redcircle.Http;
import android.content.Context;
import java.util.HashMap;
/**
* Created by Administrator 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;
}
/**
* 请求报表数据
* @param params 包含手机的imei码,用户名username,包含当前时间time
* @param onHTTPConnectionListener 网络连接的回调方法
*/
public void requestChart(HashMap<String,String>params,MyHttp.OnHTTPConnectionListener onHTTPConnectionListener){
//把params转换成Json
MyHttp.newInstance().addRequest(NetWorkConfig.URL_REQUEST,params,onHTTPConnectionListener);
}
public static void login(HashMap<String,String> params,MyHttp.OnHTTPConnectionListener onHTTPConnectionListener){
MyHttp.newInstance().addRequest(NetWorkConfig.URL_LOGIN, params, onHTTPConnectionListener);
}
public static void regester(){
}
}
MainActivity
package com.redcircle.redcircle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.gson.Gson;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.redcircle.redcircle.Http.MyAllHttpMethod;
import com.redcircle.redcircle.Http.MyHttp;
import com.redcircle.redcircle.Http.NetWorkConfig;
import com.redcircle.redcircle.dao.NewSales;
import com.redcircle.redcircle.utils.GsonUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends BaseActivity{
private Button mButtonNet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonNet = (Button) findViewById(R.id.button_net_contect);
mButtonNet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
HashMap<String,String> params = new HashMap<String, String>();
MyAllHttpMethod.newInstance().requestChart( params, new MyHttp.OnHTTPConnectionListener() {
@Override
public void NullInternet() {
Toast.makeText(MainActivity.this, "无网络连接,请打开网络", Toast.LENGTH_SHORT).show();
}
@Override
public void FailConnection(int errocode) {
Toast.makeText(MainActivity.this, "网络连接错误", Toast.LENGTH_SHORT).show();
}
@Override
public void SuccessConnection(String response) {
Toast.makeText(MainActivity.this, "网络连接成功:" + response, Toast.LENGTH_SHORT).show();
try {
JSONObject object = new JSONObject(response);
JSONArray array = object.getJSONArray("data");
// List<NewSales> list = new ArrayList<NewSales>();
List<NewSales> list = GsonUtils.readJsonArray(array,NewSales.class);
DbUtils.create(getApplication(),"").saveAll(list);
for (NewSales sales:list){
Log.d("sales","时间"+sales.getTime()+"数量"+sales.getNum());
}
} catch (JSONException e) {
e.printStackTrace();
} catch (DbException e) {
e.printStackTrace();
}
}
});
}
});
}
}
GsonUtils
package com.redcircle.redcircle.utils;
import com.google.gson.Gson;
import com.umeng.message.proguard.T;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator 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++){
try {
T t = gson.fromJson(array.getJSONObject(i).toString(),entityType);
list.add(t);
} catch (JSONException e) {
e.printStackTrace();
}
}
return list;
}
public static <T> List<T> readJsonArray(String array,Class<T> entityType)throws JSONException{
Gson gson = new Gson();
JSONArray jsonArray = new JSONArray(array);
List<T> list = new ArrayList<>();
for (int i =0;i<array.length();i++){
try {
T t = gson.fromJson(jsonArray.getJSONObject(i).toString(),entityType);
list.add(t);
} catch (JSONException e) {
e.printStackTrace();
}
}
return list;
}
}
NewSales
package com.redcircle.redcircle.dao;
/**
* Created by Administrator on 2015/10/12.
*/
public class NewSales {
private String time;
private 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;
}
}
此外,还需要搭建一个服务器,以从服务器上获得数据。(以后给出)