1:get方式
list_date = new ArrayList<HashMap<String,Object>>(); packageid_packagedata = new HashMap<Integer,HashMap<String,Object>>(); JSONObject json_book; JSONObject temp_book; JSONArray son_book = null; //int currentpackageid = packagelistid.get(j); try { json_book = new JSONObject(getRequest(domain+"/api/UserPackages?pageSize=200&phone="+phonenumber, new DefaultHttpClient(new BasicHttpParams()))); temp_book = json_book.getJSONObject("paramz"); son_book = temp_book.getJSONArray("packages"); for(int i=0;i<son_book.length();i++){ JSONObject jsonObject2_book = (JSONObject)son_book.opt(i); HashMap<String,Object> map = new HashMap<String,Object>(); map.put("thumb", domain+"/"+jsonObject2_book.getString("thumb").trim()); //1 map.put("ItemImage", domain+"/"+jsonObject2_book.getString("cover").trim()); //1 map.put("ItemTitle", URLDecoder.decode(jsonObject2_book.getString("packagename"),"utf-8")); map.put("packageid", jsonObject2_book.getInt("packageid")); map.put("orders", jsonObject2_book.getInt("orders"));//定购人数 map.put("stars", jsonObject2_book.getInt("stars"));//定购人数 map.put("uploaddate", jsonObject2_book.getString("uploaddate")); map.put("summary", URLDecoder.decode(jsonObject2_book.getString("summary"),"utf-8")); packageid_packagedata.put(jsonObject2_book.getInt("packageid"), map); list_date.add(map); } } catch (Exception e) { //Toast.makeText(getApplicationContext(),"error occused...", Toast.LENGTH_SHORT).show(); e.printStackTrace(); }
getRequest方法定义
protected static String getRequest(String url, DefaultHttpClient client) throws Exception { String result = null; int statusCode = 0; HttpGet getMethod = new HttpGet(url); try { //getMethod.setHeader("User-Agent", USER_AGENT); HttpResponse httpResponse = client.execute(getMethod); //statusCode == 200 正常 statusCode = httpResponse.getStatusLine().getStatusCode(); //处理返回的httpResponse信息 result = retrieveInputStream(httpResponse.getEntity()); } catch (Exception e) { throw new Exception(e); } finally { getMethod.abort(); } return result; }
2:post方法
protected static boolean addfav(String phone,String bookid,String packageid,String chapterid) { /*****/ boolean isfav = false; HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(SplashScreen.domain+"/api/FavoriteAdd"); try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("phone", phone)); nameValuePairs.add(new BasicNameValuePair("bookId", bookid));//用户id nameValuePairs.add(new BasicNameValuePair("packageId", packageid)); nameValuePairs.add(new BasicNameValuePair("chapterId", chapterid)); //根据package名字获取packageid //String tempstr = (String)mData.get(position).get("title"); //int tempint = SplashScreen.packageset.get(tempstr); //nameValuePairs.add(new BasicNameValuePair("id", String.valueOf(tempint))); //packageid httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); //statusCode = httpResponse.getStatusLine().getStatusCode(); //处理返回的httpResponse信息 String result = retrieveInputStream(response.getEntity()); JSONObject json_book; JSONObject temp_book; JSONArray son_book = null; int code; String message; String status; try { json_book = new JSONObject(result); status = json_book.optString("status"); if(status.equals("ok")) { isfav = true; } else { isfav = false; //Toast.makeText(getApplicationContext(),"收藏失败", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { //Toast.makeText(getApplicationContext(),"error occused...", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } //根据 response返回结果判断是否成功执行提交 } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } return isfav; }
retrieveInputStream方法定义
protected static String retrieveInputStream(HttpEntity httpEntity) { int length = (int) httpEntity.getContentLength(); //the number of bytes of the content, or a negative number if unknown. If the content length is known but exceeds Long.MAX_VALUE, a negative number is returned. //length==-1,下面这句报错,println needs a message if (length < 0) length = 10000; StringBuffer stringBuffer = new StringBuffer(length); try { InputStreamReader inputStreamReader = new InputStreamReader(httpEntity.getContent(), HTTP.UTF_8); char buffer[] = new char[length]; int count; while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) { stringBuffer.append(buffer, 0, count); } } catch (UnsupportedEncodingException e) { } catch (IllegalStateException e) { } catch (IOException e) { } return stringBuffer.toString(); }