如何调用http接口获取json数据及GET/POST方式调用http接口

本文介绍了如何使用HTTP接口获取JSON数据,包括GET和POST两种调用方式。通过实例详细讲解了接口调用的步骤和方法,帮助读者掌握从接口获取并解析JSON数据的核心技巧。
摘要由CSDN通过智能技术生成


接口调用方法,这里返回的是JSONObject:

  public JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {  
		/*
		JSONObject jsonObject = null;  
		try {    
			final URL url = new URL(requestUrl); 
            
            //创建代理服务器
    	    InetSocketAddress addr = new InetSocketAddress("dl-proxy.neusoft.com", 8080);
    	    Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); //http代理
    		Authenticator.setDefault(new MyAuthenticator("liuxinyi", "N@6228279liu")); //设置代理的用户和密码
    		final HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy); //设置代理访问
    		
			
    		//不用代理  
    		final HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

    		//设置请求方式(GET/POST)  
            conn.setRequestMethod(requestMethod);  
    		conn.setDoOutput(true);  
            conn.setDoInput(true);  
            conn.setUseCaches(false);  
            conn.setRequestProperty("Content-Type", "text/html;charset=utf-8");
	   
	        //当outputStr不为null时向输出流写数据  
	        if (null != outputStr) {  
		        OutputStream outputStream = conn.getOutputStream();  
		        outputStream.write(outputStr.getBytes("UTF-8"));  
			    outputStream.close();  
		    }  
	   
		    //从输入流读取返回内容  
	        final BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
	        String str = null;  
	        StringBuffer buffer = new StringBuffer();  
	             while ((str = br.readLine()) != null) {  
	                 buffer.append(str);  
	        }  
	        //释放资源  
			br.close();
	        conn.disconnect();  
	        jsonObject = JSONObject.fromObject(buffer.toString()); 
	        
		}catch (ConnectException ce) {
			ce.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  
		*/
		
		JSONObject jsonObject = null;
		try {
			HttpClient client = new HttpClient();
			HttpMethod httpMethod = null;
			if ("get".equalsIgnoreCase(requestMethod) || StringUtils.isBlank(requestMethod)) {
				httpMethod = new GetMethod(requestUrl);
			} else if ("post".equalsIgnoreCase(requestMethod)) {
				httpMethod = new PostMethod(requestUrl);
				((PostMethod)httpMethod).setRequestBody(outputStr);
			}
			httpMethod.setRequestHeader("Content-Type", "text/html;charset=utf-8");
			int code = client.executeMethod(httpMethod);
			if (code == 200) {
				jsonObject = JSONObject.fromObject(httpMethod.getResponseBodyAsString());
			}
			httpMethod.releaseConnection();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	    return jsonObject;
	}
  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值