先写服务器端的代码,先写一个person对象
04 | private String address; |
11 | public void setId( int id) { |
14 | public String getName() { |
17 | public void setName(String name) { |
20 | public String getAddress() { |
23 | public void setAddress(String address) { |
24 | this .address = address; |
27 | public String toString() { |
28 | return "Person [id=" + id + ", name=" + name + ", address=" + address |
31 | public Person( int id, String name, String address) { |
35 | this .address = address; |
初始化一下person对象
1 | public class JsonService { |
3 | public Person getPerson(){ |
4 | Person person= new Person( 1001 , "hang" , "suqian" ); |
服务器端json的工具类
01 | public class JsonToos { |
02 | public static String CreateJsonString(String key,Object value){ |
04 | JSONObject jsonObject= new JSONObject(); |
05 | jsonObject.put(key,value); |
06 | return jsonObject.toString(); |
服务端的servlet
01 | public class JsonAction extends HttpServlet { |
05 | private static final long serialVersionUID = 1L; |
06 | private JsonService service; |
08 | * @see HttpServlet#HttpServlet() |
16 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
18 | doPost(request, response); |
22 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
23 | response.setContentType( "text/html;charset=utf-8" ); |
24 | request.setCharacterEncoding( "utf-8" ); |
25 | response.setCharacterEncoding( "utf-8" ); |
26 | PrintWriter out=response.getWriter(); |
28 | String jsonString=JsonToos.CreateJsonString( "person" ,service.getPerson()); |
29 | out.println(jsonString); |
35 | public void init() throws ServletException { |
36 | service= new JsonService(); |
下面为android端的代码
注意
<uses-permission android:name="android.permission.INTERNET"/>
这个一定要加的,网络访问权限
定义person的对象同上
下面是http的工具类,主要是http访问,获取json的字符串
01 | public class HttpUtils { |
02 | public static String getJsonContent(String path){ |
04 | URL url= new URL(path); |
05 | HttpURLConnection connection=(HttpURLConnection) url.openConnection(); |
06 | connection.setConnectTimeout( 3000 ); |
07 | connection.setRequestMethod( "GET" ); |
08 | connection.setDoInput( true ); |
09 | int code=connection.getResponseCode(); |
11 | return changeInputString(connection.getInputStream()); |
13 | } catch (Exception e) { |
19 | private static String changeInputString(InputStream inputStream) { |
22 | ByteArrayOutputStream outPutStream= new ByteArrayOutputStream(); |
23 | byte [] data= new byte [ 1024 ]; |
26 | while ((len=inputStream.read(data))!=- 1 ){ |
27 | outPutStream.write(data, 0 , len); |
29 | jsonString= new String(outPutStream.toByteArray()); |
31 | } catch (Exception e) { |
下面为解析json字符串,把值赋给person对象
01 | public class JsonTools { |
03 | public static Person getPerson(String key, String jsonString) { |
04 | Person person = new Person(); |
07 | JSONObject jsonObject = new JSONObject(jsonString); |
08 | JSONObject personObject = jsonObject.getJSONObject(key); |
10 | person.setId(personObject.getInt( "id" )); |
11 | person.setName(personObject.getString( "name" )); |
12 | person.setAddress(personObject.getString( "address" )); |
13 | } catch (Exception e) { |
19 | public static List<Person> getPersons(String key, String jsonString) { |
20 | List<Person> list = new ArrayList<Person>(); |
22 | JSONObject jsonObject = new JSONObject(jsonString); |
23 | JSONArray jsonArray = jsonObject.getJSONArray(key); |
24 | for ( int i = 0 ; i < jsonArray.length(); i++) { |
25 | JSONObject jsonObject2 = jsonArray.getJSONObject(i); |
26 | Person person = new Person(); |
27 | person.setId(jsonObject2.getInt( "id" )); |
28 | person.setName(jsonObject2.getString( "name" )); |
29 | person.setAddress(jsonObject2.getString( "address" )); |
32 | } catch (Exception e) { |
38 | public static List<String> getlistString(String key, String jsonString) { |
39 | List<String> list = new ArrayList<String>(); |
41 | JSONObject jsonObject = new JSONObject(jsonString); |
42 | JSONArray jsonArray = jsonObject.getJSONArray(key); |
43 | for ( int i = 0 ; i < jsonArray.length(); i++) { |
45 | String msg = jsonArray.getString(i); |
49 | } catch (Exception e) { |
56 | public static List<Map<String,Object>> getlistMap(String key, String jsonString){ |
57 | List<Map<String,Object>> list= new ArrayList<Map<String,Object>>(); |
59 | JSONObject jsonObject = new JSONObject(jsonString); |
60 | JSONArray jsonArray = jsonObject.getJSONArray(key); |
61 | for ( int i = 0 ; i < jsonArray.length(); i++) { |
62 | JSONObject jsonObject2 = jsonArray.getJSONObject(i); |
63 | Map<String,Object> map= new HashMap<String, Object>(); |
64 | Iterator<String> iterator=jsonObject2.keys(); |
66 | while (iterator.hasNext()){ |
67 | String json_key=iterator.next(); |
68 | Object json_value=jsonObject2.get(json_key); |
72 | map.put(json_key, json_value); |
android点击button触发的事件
02 | public void onClick(View v) { |
06 | path= "http://192.168.123.1/http/JsonAction" ; |
07 | String jsonstring=HttpUtils.getJsonContent(path); |
08 | Log.i(TAG, "1212" +jsonstring); |
10 | Person person=JsonTools.getPerson( "person" , jsonstring); |
11 | Log.i(TAG,person.toString()); |