客户端访问服务器接收显示一条数据
客户端向服务器发请求然后信息返回到客户端,在本地显示出来显示在一个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=" 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=" 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>
以上就是在客户端获取服务器的信息