自己搭建tomcat服务器生成json数据,并用android客户端获取

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u012539700/article/details/80392608

服务端index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<p> json test </p>
	
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>

<json:object>
  <json:property name="itemCount" value="100"/>
  <json:property name="subtotal" value="$15.50"/>
  <json:array name="items" var="item" items="1">
    <json:object>
      <json:property name="title" value="The big book of foo"/>
      <json:property name="description" value="it does not work"/>
      <json:property name="imageUrl" value="http://www.baidu.com"/>
      <json:property name="price" value="$100"/>
      <json:property name="qty" value="1"/>
    </json:object>
    <json:object>
      <json:property name="title" value="The big book of foo2"/>
      <json:property name="description" value="it does not work2"/>
      <json:property name="imageUrl" value="http://www.baidu.com2"/>
      <json:property name="price" value="$1002"/>
      <json:property name="qty" value="12"/>
    </json:object>
  </json:array>
</json:object>
</body>
</html>

服务端生成的json:

{"itemCount":"100","subtotal":"$15.50","items":[{"title":"The big book of foo","description":"it does not work","imageUrl":"http://www.baidu.com","price":"$100","qty":"1"},{"title":"The big book of foo2","description":"it does not work2","imageUrl":"http://www.baidu.com2","price":"$1002","qty":"12"}]} 

android端解析代码:

package com.excellence.exampleapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ParseJsonActivity extends Activity {
    private TextView textView = null;
    private StringBuilder sb  = new StringBuilder();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parse_json);
        textView = findViewById(R.id.text);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                   final String jsonString =  readParse("http://10.1.1.155:8080/JsonToAndroid/index.json");
                    JSONObject object = new JSONObject(jsonString);
                    String itemCount =  object.getString("itemCount");
                    sb.append("\n"+"itemcount : "+itemCount+"\n");
                    Log.d("jxdJson","itemcount : "+itemCount);
                    String subtotal = object.getString("subtotal");
                    sb.append("subtotal : "+subtotal+"\n");
                    Log.d("jxdJson","subtotal : "+subtotal);

                    JSONArray jsonArray = object.getJSONArray("items");
                    Log.d("jxdJson","jsonArray length : "+jsonArray.length());
                    for(int i = 0; i<jsonArray.length();i++){
                        JSONObject arrayObject = jsonArray.getJSONObject(i);
                        String title = arrayObject.getString("title");
                        Log.d("jxdJson","title : "+title);
                        sb.append("\n"+"title : "+title+"\n");

                        String description = arrayObject.getString("description");
                        Log.d("jxdJson","description : "+description);
                        sb.append("\n"+"description : "+description+"\n");

                        String imageUrl = arrayObject.getString("imageUrl");
                        Log.d("imageUrl","imageUrl : "+imageUrl);
                        sb.append("\n"+"imageUrl : "+imageUrl+"\n");

                        String price = arrayObject.getString("price");
                        Log.d("jxdJson","price : "+price);
                        sb.append("\n"+"price : "+price+"\n");

                        String qty = arrayObject.getString("qty");
                        Log.d("jxdJson","qty : "+qty);
                        sb.append("\n"+"qty : "+qty+"\n");

                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(jsonString);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }
    /**
     * 从指定的URL中获取数组
     * @param urlPath
     * @return
     * @throws Exception
     */
    public static String readParse(String urlPath) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        URL url = new URL(urlPath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream inStream = conn.getInputStream();
        while ((len = inStream.read(data)) != -1) {
            outStream.write(data, 0, len);
        }
        inStream.close();
        return new String(outStream.toByteArray());//通过out.Stream.toByteArray获取到写的数据
    }
}


展开阅读全文

没有更多推荐了,返回首页