android 知识 post get 获取json

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();
        
        }
posted on 2012-10-28 16:11  人的本质是什么? 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/PirateCaptain/articles/2743689.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值