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

服务端index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


	

json test

<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>

服务端生成的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


你可能感兴趣的:(移动开发,Android)