JSON

JSON学习三――网下项目验证(通过)

字号+ 作者:H5之家 来源:H5之家 2018-04-23 09:00 我要评论( )

JSON学习三――网上项目验证(通过) 服务器端:Eclipse + tomcat /*@author:LKP *company:mobiscloud *2011-5-16 */ public class JSONDemoServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 12323L; private ListUse

JSON学习三――>网上项目验证(通过)
服务器端:Eclipse + tomcat

     /*@author:LKP 
*company:mobiscloud
*2011-5-16
*/
public class JSONDemoServlet extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 12323L;
private List<User> list;

/**
* 处理post方式提交的数据
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

/**
* 出来get方式提交的数据
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.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) {
}

array.add(obj);
}

// 输出
out.write(array.toString());
out.flush();
out.close();
}

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);
}

这些都是服务器端的程序,不是很多,比较好了解,前提是一定要搭建好server环境。


Client端程序:

         public class MyJSON extends Activity {
TextView tv;
    private static final String TAG = "MyJSON";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);
try {
StringBuffer sb = new StringBuffer();
// 在测试过程中,经常是用本机做测试服务器,访问本机的IP地址要设置为192.168.1.195
String url = "http://192.168.1.195:8080/Webtest/JSONDemoServlet";
String body = getContent(url);
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");
}
tv.setText(sb.toString());
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}

}
private String getContent(String url) throws Exception{
        StringBuilder sb = new StringBuilder();
       
        HttpClient client = new DefaultHttpClient();
        HttpParams httpParams = client.getParams();
        //设置网络超时参数
        HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
        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();
}
}


注意加入联网的权限:<uses-permission android:name="android.permission.INTERNET" />

布局文件更简单了:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tv"
    />
</LinearLayout>
  

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • python数据挖掘:Json结构分析

    python数据挖掘:Json结构分析

    2018-04-23 09:00

  • JFinal 文档、资料、学习、API,概述

    JFinal 文档、资料、学习、API,概述

    2018-04-22 13:01

  • C#使用Http Post方式传递Json数据字符串调用Web Service

    C#使用Http Post方式传递Json数据字符串调用Web Service

    2018-04-22 10:01

  • springMvc配置xml使ResponseBody返回Json

    springMvc配置xml使ResponseBody返回Json

    2018-04-21 18:00

网友点评