java后台調用后台接口


后台調用后台接口並獲取返回值:

              //-----開始------------
			DefaultHttpClient httpClient = new DefaultHttpClient();
			String url="http://192.xxx.x.xxx:xxxx/xx/xx";
			HttpPost httpPost = new HttpPost(url);
			httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
              // ----傳參---- nvps.add(new BasicNameValuePair("VId",“xxx”)); nvps.add(new BasicNameValuePair("password",“xxx”)); nvps.add(new BasicNameValuePair("telephone", “xxx”)); nvps.add(new BasicNameValuePair("nickName",“xxx”)); try { httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println("0000"); // 執行請求 HttpResponse response = null; try { response = httpClient.execute(httpPost); } catch (IOException e) { System.out.println("0002"); e.printStackTrace(); } System.out.println("0003"); BufferedReader in = null; String content = null; try { System.out.println("0004"); in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } content = sb.toString(); } catch (UnsupportedOperationException | IOException e) { e.printStackTrace(); } finally { try { if(in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } }
              // 打印返回值 System.out.println(content); //------結束--------

  


免責聲明!

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



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