java請求POST發送json格式請求


 

 public static String upload(String url){
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            MultipartEntity reqEntity = new MultipartEntity();
            ArrayList<HashMap<String,String>> enclosureList = new ArrayList<HashMap<String, String>>();
            for (int i = 0; i <10 ; i++) {
                HashMap<String,String> tmpHash = new HashMap<String, String>();
                tmpHash.put("name","testfile"+i+".jpg");
                tmpHash.put("url","CgAE3FdNSROAVQqrAAD8dT1kf6k929"+i+".jpg");
                enclosureList.add(tmpHash);
            }

            JSONArray enclosure = JSONArray.fromObject(enclosureList);
            StringBody enclosure_str = new StringBody(enclosure.toString());
            //json格式的請求數據封裝
       JSONObject param
= new JSONObject(); param.put("bidId","1027228"); param.put("datumId","102"); param.put("enclosure",enclosure.toString()); System.out.println(param.toString()); StringEntity se = new StringEntity(param.toString()); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); if(statusCode == HttpStatus.SC_OK){ System.out.println("服務器正常響應....."); HttpEntity resEntity = response.getEntity();           //解析json格式的返回結果
JSONObject json
= JSONObject.fromObject(EntityUtils.toString(resEntity).toString()); System.out.println(json.toString()); EntityUtils.consume(resEntity); } } catch (Exception e) { e.printStackTrace(); } return ""; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM