标签:android style blog http java 使用 os io
“ abc ”,其格式为:"abc"。除了字符 ",\,/ 和一些控制符(\b,\f,\n,\r,\t)需要编码外,其他 Unicode 字符可以直接输出。
Boolean 类型表示为 true 或 false 。此外,JavaScript 中的 null 被表示为 null,注意,true、false和 null 都没有双引号,否则将被视为一个 String 。
JSON 还可以表示一个数组对象,使用 [] 包含所有元素,每个元素用逗号分隔,元素可以是任意的
Value,例如,以下数组包含了一个 String,Number,Boolean 和一个 null:
["abc",12345,false,null]
{"city":"Beijing","street":" Chaoyang Road ","postcode":100025}其中 Value 也可以是另一个 Object 或者数组,因此,复杂的 Object 可以嵌套表示,例如,一个
{"name":"Michael","address": {"city":"Beijing","street":" Chaoyang Road ","postcode":100025}
}
下面写一个实例:(记得服务器端要先导包)
这是在我曾经写好的代码上写的,我现在把主要代码写下。
新建一个servlet:
package cn.four.json; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.print.event.PrintServiceAttributeEvent; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import cn.four.service.Foundservice; import cn.four.service.FoundserviceImp; import cn.four.user.Found; public class JSONDemoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Foundservice fd=new FoundserviceImp(); //我这是以前写过的,继承接口 List<Found> list=fd.getfound(); //new 一个list JSONObject jsb=new JSONObject(); //new 一个JSONObject对象 jsb.put("foundlist",list); //得到转成后的数据 JSONArray fdarray=jsb.getJSONArray("foundlist"); //存到数组 response.setContentType("text/plain");//解决乱码 response.setCharacterEncoding("UTF-8"); PrintWriter out=response.getWriter(); out.write(fdarray.toString()); //关闭 out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }服务器端就这样结束了。
下面是android的介绍
Android SDK 提供了Apache 的HttpClient 类处理网络访问,相信很多读者朋友都在其他项目当中用
到过HttpClient。我写了一个方法类,获取某一网址的网页内容,代码如下:
package com.four.http; 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; public class httpcontent { public 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.example.mychat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import com.four.http.httpcontent; public class User_tips extends Activity { httpcontent content = new httpcontent(); private Button btnshua; private ListView xianshi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user_tips); btnshua = (Button) findViewById(R.id.btnshua); xianshi = (ListView) findViewById(R.id.listView1); btnshua.setOnClickListener(new shualistener()); } class shualistener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub listxianshi(); } } private List<Map<String, Object>> getmessg(String url) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); String body; try { body = content.getContent(url); JSONArray array = new JSONArray(body); for (int i = 0; i < array.length(); i++) { JSONObject obj = array.getJSONObject(i); Map<String, Object> map = new HashMap<String, Object>(); map.put("foundname", obj.getString("foundname")); map.put("foundplace", obj.getString("foundplace")); list.add(map); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } private void listxianshi(){ try { String url = "http://10.20.180.34:8080/lostfound/JSONDemoServlet"; SimpleAdapter adapter = new SimpleAdapter(this, getmessg(url), R.layout.list_item, new String[] { "foundname", "username" }, new int[] { R.id.foundname, R.id.username }); xianshi.setAdapter(adapter); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
效果图:
android之JSON 进行网络数据交换,布布扣,bubuko.com
标签:android style blog http java 使用 os io
原文地址:http://blog.csdn.net/u013007955/article/details/38750795