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); //------结束--------

  

posted @ 2018-05-10 15:03  一个九  阅读(3719)  评论(0编辑  收藏  举报