陈斌彬的技术博客

Stay foolish,stay hungry

Android webservice读取外勤通登录接口Demo

img

目录结构

img

img

MainActivity

package cc.webservice.huaxun.wqtloginwebservice;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cc.webservice.huaxun.wqtloginwebservice.adapter.LoginAdapter;
import cc.webservice.huaxun.wqtloginwebservice.model.LoginModel;
import cc.webservice.huaxun.wqtloginwebservice.utils.HttpUtils;

public class MainActivity extends AppCompatActivity {

    private ListView lvNews;
    private LoginAdapter adapter;
    private List<LoginModel> newsList;

    public static  String GET_NEWS_URL = "http://192.168.1.90:10089/DWTong/Webservice/MobileService.asmx/Login?uname=admin&pass=admin";

    private Handler getNewsHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            String jsonData = (String) msg.obj;
            String substring =jsonData.substring(74,jsonData.length());
            String resultString = substring.substring(0,substring.length()-9);

            try {
                JSONArray jsonArray = new JSONArray(resultString);

                for (int i=0;i<jsonArray.length();i++){

                    JSONObject object = jsonArray.getJSONObject(i);

                    String UserName = object.getString("UserName");
                    String UserPwd = object.getString("UserPwd");
                    String TrueName = object.getString("TrueName");
                    String Serils = object.getString("Serils");
                    String Department = object.getString("Department");
                    String JiaoSe = object.getString("JiaoSe");
                    String ActiveTime = object.getString("ActiveTime");
                    String ZhiWei = object.getString("ZhiWei");
                    String ZaiGang = object.getString("ZaiGang");
                    newsList.add(new LoginModel(UserName,UserPwd,TrueName,Serils,Department,JiaoSe,ActiveTime,ZhiWei,ZaiGang));
                    System.out.println(newsList);
                }

                adapter.notifyDataSetChanged();
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setContentView(R.layout.activity_main);

        lvNews = (ListView) findViewById(R.id.lvNews);
        newsList = new ArrayList<LoginModel>();
        adapter = new LoginAdapter(this, newsList);

        lvNews.setAdapter(adapter);
        HttpUtils.getNewsJSON(GET_NEWS_URL, getNewsHandler);
    }
}

HttpUtils

package cc.webservice.huaxun.wqtloginwebservice.utils;

import android.os.Handler;
import android.os.Message;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by wuyongmin on 16/6/14.
 */
public class HttpUtils {

    public static void getNewsJSON(final String url, final Handler handler){

        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection conn;
                InputStream is;
                try {
                    conn = (HttpURLConnection) new URL(url).openConnection();
                    conn.setRequestMethod("GET");
                    is = conn.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    String line = "";
                    StringBuilder result = new StringBuilder();
                    while ( (line = reader.readLine()) != null ){
                        result.append(line);

                    }
                    Message msg = new Message();
                    msg.obj = result.toString();
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

Model

package cc.webservice.huaxun.wqtloginwebservice.model;

/**
 * Created by wuyongmin on 16/6/14.
 */
public class LoginModel {

    private String UserName;
    private String UserPwd;
    private String TrueName;
    private String Serils;
    private String Department;
    private String JiaoSe;
    private String ActiveTime;
    private String ZaiGang;
    private String ZhiWei;

    public String getZhiWei() {
        return ZhiWei;
    }

    public void setZhiWei(String zhiWei) {
        ZhiWei = zhiWei;
    }

    public String getUserPwd() {
        return UserPwd;
    }

    public void setUserPwd(String userPwd) {
        UserPwd = userPwd;
    }

    public String getTrueName() {
        return TrueName;
    }

    public void setTrueName(String trueName) {
        TrueName = trueName;
    }

    public String getSerils() {
        return Serils;
    }

    public void setSerils(String serils) {
        Serils = serils;
    }

    public String getDepartment() {
        return Department;
    }

    public void setDepartment(String department) {
        Department = department;
    }

    public String getJiaoSe() {
        return JiaoSe;
    }

    public void setJiaoSe(String jiaoSe) {
        JiaoSe = jiaoSe;
    }

    public String getActiveTime() {
        return ActiveTime;
    }

    public void setActiveTime(String activeTime) {
        ActiveTime = activeTime;
    }

    public String getZaiGang() {
        return ZaiGang;
    }

    public void setZaiGang(String zaiGang) {
        ZaiGang = zaiGang;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public LoginModel(String userName, String userPwd,String trueName, String serils,String department, String jiaoSe,String activeTime, String zhiWei,String zaiGang){

        setUserName(userName);
        setUserPwd(userPwd);
        setTrueName(trueName);
        setSerils(serils);
        setDepartment(department);
        setJiaoSe(jiaoSe);
        setActiveTime(activeTime);
        setZhiWei(zhiWei);
        setZaiGang(zaiGang);
    }
}

LoginAdapter

package cc.webservice.huaxun.wqtloginwebservice.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cc.webservice.huaxun.wqtloginwebservice.R;
import cc.webservice.huaxun.wqtloginwebservice.model.LoginModel;

/**
 * Created by wuyongmin on 16/6/14.
 */
public class LoginAdapter extends BaseAdapter {

    private Context context;
    private List<LoginModel> newsList;

    public LoginAdapter(Context context, List<LoginModel> newsList) {
        this.context = context;
        this.newsList = newsList;
    }

    @Override
    public int getCount() {
        return newsList.size();
    }

    @Override
    public LoginModel getItem(int position) {
        return newsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.login_items, null);
        }

        TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
        TextView tvDesc = (TextView) convertView.findViewById(R.id.tvDesc);

        LoginModel news = newsList.get(position);
        tvTitle.setText(news.getUserName());
        tvDesc.setText(news.getTrueName());
        return convertView;
    }
}

Resource Reference

https://github.com/cnbin/androidDemo/tree/master/WQTLoginWebservice