可不可以详细一点啊?从服务端那边怎样从客户端这边传?我不太会用
我自己也写完了,就是图片资源不知道该怎样设置。
下面的是服务端向客户端传递字符串的格式:
List<Book> books = new ArrayList<Book>(); books = BookDao.getItem(); StringBuilder json = new StringBuilder(); json.append('['); for (Book book : books) { System.out.println(book.getDescrpition()); json.append('{'); json.append("id:").append(book.getId()).append(','); json.append("bookname:\"").append(book.getBookname()).append("\","); json.append("author:\"").append(book.getAuthor()).append("\","); json.append("price:").append(book.getPrice()).append(","); json.append("description:\"").append(book.getDescrpition()) .append("\","); json.append("picUrl:\"").append(book.getPicUrl()).append("\","); json.append("bookTypeId:").append(book.getBookTypeId()); json.append("},"); } json.deleteCharAt(json.length() - 1); json.append(']'); // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( // resp.getOutputStream())); // bw.write(json.toString()); // bw.flush(); // bw.close(); OutputStream os = resp.getOutputStream(); PrintWriter pw = new PrintWriter(os); pw.write(json.toString()); pw.flush(); pw.close();
接下来是服务端的解析:
public void jsonProcess() { String url = "http://192.168.8.25:8080/myEbookWeb/booklists"; HttpPost request = new HttpPost(url); JSONObject jsonParam = new JSONObject(); try { Log.i("info", "jsonParam" + jsonParam.toString()); StringEntity se = new StringEntity(jsonParam.toString()); request.setEntity(se); try { HttpResponse httpResponse = new DefaultHttpClient() .execute(request); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = httpResponse.getEntity(); String content = EntityUtils.toString(entity, "gbk"); if (content != null) { try { books = parseJSON(content); Log.i("info", books.size() + books.toString()); picUrl = new ArrayList<String>(); bookname = new ArrayList<String>(); prices = new ArrayList<String>(); for (Book book : books) { picUrl.add(book.getPicUrl()); bookname.add(book.getBookname()); prices.add(String.valueOf(book.getPrice())); } } catch (Exception e) { e.printStackTrace(); } } else { } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } private static List<Book> parseJSON(String jsondata) throws Exception { List<Book> books = new ArrayList<Book>(); JSONArray array = new JSONArray(jsondata); Log.d("info", jsondata); for (int i = 0; i < array.length(); i++) { JSONObject item = array.getJSONObject(i); String des = item.getString("description"); String picUrl = item.getString("picUrl"); int typeId = item.getInt("bookTypeId"); books.add(new Book(item.getInt("id"), item.getString("bookname"), item.getDouble("price"), item.getString("author"), des, picUrl, typeId)); } return books; }
还有一个图片没办法设置,谁有办法帮我一下
@mitty: 图片也是来自服务端?还是android 应用的资源图片?
如果是服务端的图片,可以给个url啊,android 应用 用个图片显示控件,显示这个图片!!
是服务端的图片,得到url了,但具体怎样显示到控件里我不太会用,能说清楚点吗?
@mitty: 伙计,这个就得需要你自己解决了!查看相关的API吧,Android 开发包里面的Demo挺好的,可以参考一下!
ok,谢了