使用 HttpURLConnection 获取不到网络数据

9 篇文章 0 订阅
5 篇文章 0 订阅

HttpURLConnection 通常在新开的一个线程内进行活动,有一个小细节需要重视,否则获不到网络数据。

new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				HttpURLConnection connection = null;
				try {
					URL url = new URL("www.baidu.com");//注意这里
					connection = (HttpURLConnection)url.openConnection();
					connection.setRequestMethod("GET");
					connection.setConnectTimeout(8000);
					connection.setReadTimeout(8000);
					InputStream in = connection.getInputStream();
					BufferedReader reader = new BufferedReader(new InputStreamReader(in));
					StringBuilder response = new StringBuilder();
					String line;
					while((line = reader.readLine()) != null){
						response.append(line);
					}
					Message message = new Message();
					message.what = SHOW_RESPONSE;
					message.obj = response.toString();
					handler.sendMessage(message);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}finally{
					if(connection != null){
						connection.disconnect();
					}
				}
			}
		}).start();

由于现在的浏览器非常智能,我们输入网址的时候不需要加前面的几个字母和符号,将这个习惯带进代码中就不行了,应该改为:

URL url = new URL("http://www.baidu.com");
这样就能获得数据了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你不想将参数添加到URL中,而是在请求时添加参数,可以使用HttpURLConnection并将参数添加到请求的消息体中。 以下是使用HttpURLConnection进行POST请求并添加参数的示例代码: ```java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; public class ApiClient { public static void main(String[] args) { try { // 创建URL对象 URL url = new URL("http://api.example.com/data"); // 替换为实际的API地址 // 打开连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法为POST connection.setRequestMethod("POST"); // 设置请求头 connection.setRequestProperty("Content-Type", "application/json"); // 启用输出流 connection.setDoOutput(true); // 添加参数到请求的消息体中 String jsonParam = "{\"name\":\"jjw-test-workspace2\"}"; byte[] postData = jsonParam.getBytes(StandardCharsets.UTF_8); try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) { outputStream.write(postData); outputStream.flush(); } // 获取响应数据 StringBuilder response = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String line; while ((line = reader.readLine()) != null) { response.append(line); } } // 在这里使用获取到的响应数据进行后续处理 // ... // 关闭连接 connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上述示例中,我们使用HttpURLConnection发送了一个POST请求,并将参数添加到请求的消息体中。首先,我们创建了一个URL对象,并通过调用`openConnection()`方法打开连接。然后,我们设置请求方法为POST,设置请求头为`Content-Type: application/json`,并启用输出流。 接下来,我们将参数`{\"name\":\"jjw-test-workspace2\"}`转换为字节数组,并使用`DataOutputStream`将其写入连接的输出流中。 最后,我们获取响应数据,并在这里使用获取到的数据进行后续处理。 请确保将实际的API地址和参数替换为示例中的占位符。 希望对你有所帮助!如果你还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值