android-servlet 返回json
需要在 jsp web 项目中添加jar 有
json-lib-2.2.3-jdk15.jar
commons-beanutils-1.7.0.jar
commons-httpclient-3.1.jar
commons-lang-2.3.jar
commons-logging-1.1.1.jar
commons-collections-3.2.1.jar
ezmorph-1.0.3.jar
commons-beanutils-1.7.0.jar
commons-httpclient-3.1.jar
commons-lang-2.3.jar
commons-logging-1.1.1.jar
commons-collections-3.2.1.jar
ezmorph-1.0.3.jar
如果没有会报错
java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher
java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap
java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean
java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException
java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap
java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean
java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException
java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
还要有
json-lib.jar
servlet
public class Listservlet extends HttpServlet { private VideoNewsService vs = new VideoNewsServiceImpl(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<VideoNews> videoNews = vs.readNews(); JSONArray jsonarr = JSONArray.fromObject(videoNews ); response.setCharacterEncoding( "UTF-8"); // 设置头文件 response.setContentType( "text/plain;charset=UTF-8"); response.setHeader( "Pragma", "No-cache" ); response.setHeader( "Cache-Control", "no-cache" ); response.setDateHeader( "Expires", 0); response.getWriter().print(jsonarr); } }
解析json
import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONObject; import com.example.jsontest.VideoNews; public class VideoNewsServiceImpl implements VideoNewsService { public static void main(String[] args) throws Exception { new VideoNewsServiceImpl().readNews(); } public List<VideoNews> readNews() throws Exception { ArrayList<VideoNews> lst = new ArrayList<VideoNews>(); String path = "http://pc201310290345:8080/jsonweb/Listservlet" ; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod( "GET"); if (200 == conn.getResponseCode()) { InputStream instream = conn.getInputStream(); System. out.println(instream); lst = parseJSON(instream); } return lst; } private ArrayList<VideoNews> parseJSON(InputStream instream) throws Exception { ArrayList<VideoNews> lst = new ArrayList<VideoNews>(); // inoutStream 转换成byte[] byte[] data = IOUtils.read(instream); String jsonStr = new String(data,"UTF-8" ); // String 准换成json JSONArray array = new JSONArray(jsonStr); for (int i = 0; i < array.length(); i++) { JSONObject jsonObj = (JSONObject) array.getJSONObject(i); VideoNews v = new VideoNews(jsonObj.getInt("id" ), jsonObj.getString( "title"), jsonObj.getInt("length" )); lst.add(v); } return lst; } public static byte [] read(InputStream instream) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = instream.read(buffer)) != -1) { bos.write(buffer, 0, len); } return bos.toByteArray(); } }