`

客户端访问服务器接收显示一条数据

 
阅读更多

              客户端向服务器发请求然后信息返回到客户端,在本地显示出来显示在一个ListView里面

 

 

1  客户端向服务器发请求  定义一个Activity   :WorkApprovalSHowActivity

 


import java.util.ArrayList;

import com.hwtt.android.mobileoa.R;
import com.hwtt.android.mobileoa.adapter.LeaveInfoAdapter;
import com.hwtt.android.mobileoa.bean.LeaveInfo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class WorkApprovalSHowActivity extends Activity {
 private ListView lv_leave;
 LeaveInfoAdapter lva;
 ArrayList<LeaveInfo> list = new ArrayList<LeaveInfo>();
 //  Context context;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.leave_show);
  lv_leave = (ListView) findViewById(R.id.leave_show_list);

  try {
   
   list = JSONUtil.postRequest("http://172.16.10.131:8080/workflow/workapprove!phoneFindStatus.action");

   lva = new LeaveInfoAdapter(list, this);

   lv_leave.setAdapter(lva);
   // System.out.println(mainList);
  } catch (Exception e) {
   System.out.println("网络异常");
   e.printStackTrace();
  }
 }

}
2  客户端向服务器发请求后接收到的数据保存到list里面然后进行解析JsonUtil解析

package com.hwtt.android.mobileoa.ui;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.hwtt.android.mobileoa.bean.LeaveInfo;

public class JSONUtil {
 private static final String TAG = "JSONUtil";

 /**
  * 获取json内容
  *
  * @param url
  * @return JSONArray
  * @throws JSONException
  * @throws ConnectionException
  */
 public static JSONObject getJSON(String url) throws JSONException,
   Exception {

  return new JSONObject(getRequest(url));
 }

 /**
  * 向api发送get请求,返回从后台取得的信息。
  *
  * @param url
  * @return String
  */
 protected static String getRequest(String url) throws Exception {
  return getRequest(url, new DefaultHttpClient(new BasicHttpParams()));
 }

 protected static ArrayList<LeaveInfo> postRequest(String url)
   throws Exception {
  return postRequest(url, new DefaultHttpClient());
 }

 /**
  * 向api发送get请求,返回从后台取得的信息。
  *
  * @param url
  * @param client
  * @return String
  */
 protected static String getRequest(String url, DefaultHttpClient client)
   throws Exception {
  String result = null;
  int statusCode = 0;
  HttpGet getMethod = new HttpGet(url);
  Log.d(TAG, "do the getRequest,url=" + url + "");
  try {
   // getMethod.setHeader("User-Agent", USER_AGENT);
   HttpResponse httpResponse = client.execute(getMethod);
   // statusCode == 200 正常
   statusCode = httpResponse.getStatusLine().getStatusCode();
   Log.d(TAG, "statuscode = " + statusCode);
   // httpResponse.setEntity(new
   // UrlEncodedFormEntity(null,HTTP.UTF_8));
   // 处理返回的httpResponse信息
   // result=EntityUtils.toString(httpResponse.getEntity()).trim();
   // System.out.println("aaaaaaa:"+result);
   result = retrieveInputStream(httpResponse.getEntity());
   // System.out.println("aaaa:"+result);
  } catch (Exception e) {
   Log.e(TAG, e.getMessage());
   throw new Exception(e);
  } finally {
   getMethod.abort();
  }
  return result;
 }

 protected static ArrayList<LeaveInfo> postRequest(String url,
   DefaultHttpClient client) throws Exception {
  String result = null;
  int statusCode = 0;
  ArrayList<LeaveInfo> list = new ArrayList<LeaveInfo>();
  HttpPost getMethod = new HttpPost(url);
  Log.d(TAG, "do the postRequest,url=" + url + "");
  try {
   // getMethod.setHeader("User-Agent", USER_AGENT);
   HttpResponse httpResponse = client.execute(getMethod);
   // statusCode == 200 正常
   statusCode = httpResponse.getStatusLine().getStatusCode();
   Log.d(TAG, "statuscode = " + statusCode);
   // 处理返回的httpResponse信息
   if (statusCode == 200) {

    HttpEntity entity = httpResponse.getEntity();
    result = retrieveInputStream(entity);

//实例化JSONObject  将result转换成JSONObject  类型进行获取

 

     JSONObject  jsonObj = new JSONObject(result);
    String applyname = jsonObj.getString("applyname");
    String applytime = jsonObj.getString("applytime");
                String reason1=jsonObj.getString("reason");
                String status1=jsonObj.getString("status");
                LeaveInfo bean = new LeaveInfo();
                bean.setApplyname(applyname);
    bean.setApplytime(applytime);
    bean.setReason(reason1);
    bean.setStatus(status1);
    list.add(bean);
    System.out.println();
       }

  } catch (Exception e) {
   // Log.e(TAG, e.getMessage());
   throw new Exception(e);
  } finally {
   // getMethod.abort();
  }
  return list;
 }

 /**
  * 处理httpResponse信息,返回String
  *
  * @param httpEntity
  * @return String
  */
 protected static String retrieveInputStream(HttpEntity httpEntity) {

  int length = (int) httpEntity.getContentLength();
  // the number of bytes of the content, or a negative number if unknown.
  // If the content length is known but exceeds Long.MAX_VALUE, a negative
  // number is returned.
  // length==-1,下面这句报错,println needs a message
  if (length < 0)
   length = 10000;
  StringBuffer stringBuffer = new StringBuffer(length);
  try {
   InputStreamReader inputStreamReader = new InputStreamReader(
     httpEntity.getContent(), "gb2312");
   char buffer[] = new char[length];
   int count;
   while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {
    stringBuffer.append(buffer, 0, count);
   }
  } catch (UnsupportedEncodingException e) {
   Log.e(TAG, e.getMessage());
  } catch (IllegalStateException e) {
   Log.e(TAG, e.getMessage());
  } catch (IOException e) {
   Log.e(TAG, e.getMessage());
  }
  System.out.println("resulte:" + stringBuffer.toString());
  return stringBuffer.toString();
 }

}
3   定义填充ListView的Adapter

import java.util.List;

import com.hwtt.android.mobileoa.R;
import com.hwtt.android.mobileoa.bean.LeaveInfo;

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

public class LeaveInfoAdapter extends BaseAdapter {
 List<LeaveInfo>  list;
 Context mcontext;

 public LeaveInfoAdapter(List<LeaveInfo> list, Context context) {

  this. list = list;
  this.mcontext = context;
 }

 public void setResult(List<LeaveInfo> list, Context context) {
  this. list = list;
  this.mcontext = context;
 }

 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return list.size();
 }

 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
  return  list.get(position);
 }

 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  if (convertView == null) {
   LayoutInflater inflater = LayoutInflater.from(mcontext);
   convertView = inflater.inflate(R.layout.leave, null);
  }
  TextView tv_name = (TextView) convertView.findViewById(R.id.leave_name);
  tv_name.setText( "姓名:"+list.get(position).getApplyname());
  TextView tv_time = (TextView) convertView.findViewById(R.id.leave_time);
  tv_time.setText("请假时间"+ list.get(position).getApplytime());
  TextView tv_reason = (TextView) convertView
    .findViewById(R.id.leave_reason);
  tv_reason.setText( "请假原因"+list.get(position).getReason());
  TextView tv_status = (TextView) convertView
    .findViewById(R.id.leave_status);
  tv_status.setText( "状态:"+list.get(position).getStatus());

  return convertView;
 }

}
4   定义实体对象LeaveInfo

public class LeaveInfo {
 private String applyname  ;
 private String applytime  ;
 private String createTime ;
 private String creator ;
 private int id ;
 private String processDefinitionId ;
 private String props ;
 private String reason ;
 private String status  ;

 public LeaveInfo() {
  super();
  // TODO Auto-generated constructor stub
 }

 public LeaveInfo(String applyname, String applytime, String createTime,
   String creator, int id, String processDefinitionId, String props,
   String reason, String status) {
  super();
  this.applyname = applyname;
  this.applytime = applytime;
  this.createTime = createTime;
  this.creator = creator;
  this.id = id;
  this.processDefinitionId = processDefinitionId;
  this.props = props;
  this.reason = reason;
  this.status = status;
 }

 public String getApplyname() {
  return applyname;
 }

 public void setApplyname(String applyname) {
  this.applyname = applyname;
 }

 public String getApplytime() {
  return applytime;
 }

 public void setApplytime(String applytime) {
  this.applytime = applytime;
 }

 public String getCreateTime() {
  return createTime;
 }

 public void setCreateTime(String createTime) {
  this.createTime = createTime;
 }

 public String getCreator() {
  return creator;
 }

 public void setCreator(String creator) {
  this.creator = creator;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getProcessDefinitionId() {
  return processDefinitionId;
 }

 public void setProcessDefinitionId(String processDefinitionId) {
  this.processDefinitionId = processDefinitionId;
 }

 public String getProps() {
  return props;
 }

 public void setProps(String props) {
  this.props = props;
 }

 public String getReason() {
  return reason;
 }

 public void setReason(String reason) {
  this.reason = reason;
 }

 public String getStatus() {
  return status;
 }

 public void setStatus(String status) {
  this.status = status;
 }

 
}
4  ListView的XML文件  leave_show

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
   
<ListView
    android:id="@+id/leave_show_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   
    ></ListView>
</LinearLayout>

 

5  填充Adapter的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/leave_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        
        android:textColor="#000000"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/leave_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/leave_reason"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/leave_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#000000" />
 
</LinearLayout>

 

以上就是在客户端获取服务器的信息

 

分享到:
评论

相关推荐

    QT之TCP传输客户端和服务器端互发信息的实现

    2)客户端往服务器发送输入的需要发送的信息,在服务器的接收显示界面显示; 3)服务器往客户端发送输入的需要发送的信息,在客户端的接收界面显示; 4)断开连接功能,点击断开按钮实现服务器与客户端断开连接,...

    浅谈Android客户端与服务器的数据交互总结

    HTTP通信:即使用HTTP协议进行通信,工作原理是客户端向服务器端发送一条HTTP请求,服务器收到之后先解析客户端的请求,之后会返回数据给客户端,然后客户端再对这些数据进行解析和处理。HTTP连接采取的是“请求—...

    客户端和服务器端TCP协议编写的通讯软件

    通过TCP协议开发局域网监测软件。

    TCP/IP实现数据接收并保存到SQLSERVER数据库

    这是一个TCP/IP的程序,通过ONRECEIVE接收客户端连接,接收数据进行显示,并且能够显示到界面上,把符合条件的数据保存到SQLSERVER数据库中。其中,还有LISTBOX的横向滚动条的显示的内容,每次接收的内容滚动到控件...

    TCP/IP协议通讯数据发送接收(客户端服务器端都包含在内)

    面向连接是TCP在正式进行通讯之前首先通过一些握手机制确认双方通讯意向并建立一条认为可以传输的概念通道。字节流服务是TCP传输的最小单位为字节,认为字节是有意义的单位。并且TCP不会在此字节流中插入记录符号。...

    通用数据挖掘系统

    此系统为C/S结构,分为客户端/服务器端,客户端负责定期的采集计费日志文件,并将采集后的结果发送给服务器端,服务器端负责保存数据...设计了一条关联了线程的信号和界面槽,用来完成发送数据并且发送信号通 知界面。

    用VC++编制FTP客户端应用程序

    FTP协议将使用两条单独的TCP连接,一条专用于发送FTP命令,另一条则专用于传 递数据。初始建立连接时,服务器在21号端口上接收来自客户端的命令连接。当需要传 送数据时(文件列表、文件数据等),客户端向服务器...

    python中的socket实现ftp客户端和服务器收发文件及md5加密文件

    发送一条信息给服务器告诉它准备好接收了 接收文件数据 打印全部接收的提示信息 client具体实现的代码如下: # Author: Mr.Xue # 2019.10.29 # socket_ftp_client.py import socket import hashlib client = ...

    基于TCP协议网络socket编程-简单聊天机器人(java实现C/S通信)

    ②客户端程序每发送一条信息给服务器,服务器接收并回送该信息到客户端,客户端接收并显示该信息; ③当客户端发送"bye",则结束对话。 4、博文教程更详细:...

    java实现即时通讯代码

    消息,客户端登录是向服务器发送一条登录命令,客户端向服务器发送一条消息首先需要包装成定义的消息格式, 然后再发送给服务器。 不管是发送消息还是发送命令其实本质都是一条消息,向服务器发送的消息都必须按照...

    netty服务器通讯说明: 服务器条件NETTY框架编程: 服务器IP:192.168.2.106 端口8810

    数据传输的方式:从串口接收到一条完整的协议数据,计算出数据字节长度,打包成HtAlingProtocol类,并发送给服务器; package com.htcharge.nettyclient.handler; //设置连接服务器的IP和端口 public static String ...

    仿QQ的TCP通信程序(客户端)

    读取客户名并将其添加到在线客户列表,同时分配给客户一个客户编号(1,2,3•••),将该编号加在服务器名信息头部发送给客户端,客户端提取编号并读取服务器名,之后在每一条发送给服务器的信息头部都加上自己的编号...

    服务器概要设计说明.docx

    连接生命周期的管理 C++语言没有对象回收〔GC〕机制,生命周期的管理和防止内存泄露需要程序自己实现,而一条连接从产生后到销毁的过程中会有多个线程同时对其进行操作,同时读写甚至同时关闭,对象的多线程同步也需要...

    一个超级简单的网络通信例子(MFC,可作为大学课程设计)

    这个是我当年大学期间的一个课程设计的程序,代码...TCP客户端,UDP服务器也是创建一条线程来专门负责接收数据. 在接收到数据之后,通过自定义消息通知主界面,由主界面进行处理(实际上是显示) 至于其他,没什么好说的了...

    python网络编程调用recv函数完整接收数据的三种方法

    在使用socket进行网络编程中,如何判定对端发送一条报文是否接收完成,是进行socket网络开发必须要考虑的一个问题。这里,简要介绍一下判别数据接收接收完成常用的三种方法: 1.基础数据接收法: 使用基础数据接收法...

    Android移动开发-使用多线程进行网络聊天室通信的实现

    另一条负责读取Socket对应的输入流中的数据(从服务器发送过来的数据),并负责将这些数据在程序界面上显示出来。 客户端程序是一个Android应用,因此需要创建一个Android项目,这个Android应用的界面中包含两个...

    仿QQ的TCP通信程序(服务器)

    读取客户名并将其添加到在线客户列表,同时分配给客户一个客户编号(1,2,3•••),将该编号加在服务器名信息头部发送给客户端,客户端提取编号并读取服务器名,之后在每一条发送给服务器的信息头部都加上自己的编号...

    一个面向局域网、互联网的即时聊天程序源代码

    2.服务器接收到客户端发出的消息后,根据信息头标志(.msg)判断这是一则聊天信息,同时根据信息结束标志( ||)判断该信息是否完整(若不完整,写入缓冲字符串数组)、获取信息的接收用户。判断完毕后,找到对应用户...

    浅析nodejs实现Websocket的数据接收与发送

    在WebSocket API中,浏览器和服务器只需要要做一个握手(handshaking)的动作,然后,浏览器和服务器之间就形成了一条快速通道。两者之间就直接可以数据互相传送。 WebSocket是一个通信的协议,分为服务器和客户端。...

    Ajax网页聊天系统

    6 服务器接收到在线更新请求,更新请求用户向服务器最后访问时间,更新用户列表,返回更新后的用户列表,和相对当前客户端最新信息。 7 客户端收到返回信息,若包含更新后的聊天信息或用户则在页面更新聊天信息或...

Global site tag (gtag.js) - Google Analytics