日期:2014-05-16 浏览次数:20350 次
res.setContentType("UTF-8"); PrintWriter pw = res.getWriter(); JSONObject json = new JSONObject(); json.put("name","fwz"); pw.write(json.toString());
BufferedReader bufferedReader = null; StringBuilder sb = new StringBuilder(); String s = ""; try { HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(new HttpGet("http://10.0.2.2:8080/myFirstServlet/JSONServer")); HttpParams httpParams = client.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 3000); HttpConnectionParams.setSoTimeout(httpParams, 5000); HttpEntity entity = response.getEntity(); if (entity!=null) { bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8")); //8192 while((s=bufferedReader.readLine())!=null) { sb.append(s); } } nameTextView.setText(sb.toString()); //这里输出得到的JSon数据(未解析) } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }
HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://10.0.2.2:8080/myFirstServlet/JSONServer"); JSONObject jsonObject = new JSONObject(); try { jsonObject.put("name", "ze"); StringEntity entity = new StringEntity(jsonObject.toString()); post.setEntity(entity); HttpResponse responString = client.execute(post); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
try { BufferedInputStream in = new BufferedInputStream(req.getInputStream()); int i; char c; while ((i=in.read())!=-1) { c=(char)i; System.out.print(c); } System.out.println(); System.out.println("test"); } catch (Exception ex) { ex.printStackTrace(); }