服务器端和Android客户端程序都有客户端核心代码:package com.test;
import Java.io.bufferedReader;
import java.io.InputStreamReader;
import org.Apache.http.HttpEntity;
import org.apache.http.Httpresponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DEFAULTHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.textView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
StringBuffer sb=new StringBuffer();
String URL="http://192.168.1.105:8080/JSONDemoServlet/JSONServlet";
String body=getContent(url);
system.out.println("body---" body);
JSONArray array=new JSONArray(body);
for (int i = 0; i < array.length(); i ) {
JSONObject obj=array.getJSONObject(i);
sb.append("id:").append(obj.getInt("id")).append("\t");
sb.append("name:").append(obj.getString("name")).append("\r\n");
sb.append("gender:").append(obj.getString("gender")).append("\t");
sb.append("email:").append(obj.getString("email")).append("\r\n");
sb.append("------------------------\r\n");
}
TextView textView=(TextView)findViewById(R.id.textView);
textView.setText(sb.toString());
}catch (Exception e) {
// TODO: handle exception
}
}
private String getContent(String url) throws Exception{
StringBuilder sb=new StringBuilder();
HttpClient client=new DefaultHttpClient();
HttpParams httpParams=client.getParams();
//设置网络超时参数
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpResponse response=client.execute(new HttpGet(url));
HttpEntity entity=response.getEntity();
if (entity!=null) {
BufferedReader reader=new BufferedReader(new InputStreamReader(
entity.getContent(), "utf-8"), 8192);
String line=null;
while ((line=reader.readLine())!=null) {
sb.append(line "\n");
}
reader.close();
}
return sb.toString();
}
}服务器端核心代码:package com.test.Demo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arraylist;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.me.JSONArray;
import org.json.me.JSONObject;
/**
*JSONDemoServlet
*com.test.Demo
*JSONDemoServlet.java
*@author WangYongfeng
*Apr 5, 2011 5:03:49 PM TODO
*
*/
public class JSONDemoServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<user> list;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
PrintWriter out=resp.getWriter();
//准备用户数据
prepareData();
//JSON数组
JSONArray array=new JSONArray();
for(User bean:list){
//单个用户JSON对象
JSONObject obj=new JSONObject();
try{
obj.put("id", bean.getId());
obj.put("name", bean.getName());
obj.put("email", bean.getEmail());
obj.put("gender", bean.getGender());
}catch (Exception e) {
// TODO: handle exception
}
array.put(obj);
}
//输出
out.write(array.toString());
out.flush();
out.close();
}
@Override
protected void dopost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
}
private void prepareData(){
list = new ArrayList<User>();
User bean1 = new User();
bean1.setId(1001);
bean1.setName("Tony");
bean1.setEmail("tony@toeach.net");
bean1.setGender("male");
list.add(bean1);
User bean2 = new User();
bean2.setId(1002);
bean2.setName("Jack");
bean2.setEmail("jack@hotmail.com");
bean2.setGender("male");
list.add(bean2);
User bean3 = new User();
bean3.setId(1003);
bean3.setName("Marry");
bean3.setEmail("marry@163.com");
bean3.setGender("female");
list.add(bean3);
User bean4 = new User();
bean4.setId(1004);
bean4.setName("Linda");
bean4.setEmail("linda@21cn.com");
bean4.setGender("female");
list.add(bean4);
}
}
来源:搜素材网素材
搜素材网所有素材均为本站用户上传,仅供学习与参考,请勿用于商业用途,如有侵犯您的版权请联系客服服务QQ
本站提供各类html5响应式模板,前端js素材,网站模板,后台模板素材,程序源码素材。
由于技术有限本站不提供安装服务与bug修复,各类源码只提供分享服务,感谢您的理解。
如果对本站有任何意见请点击右侧侧边栏的反馈意见,我们会及时处理。