HttpURLConnection中connection.getInputStream()报异常FileNotFoundException

package com.example.myapplication; import android.util.Log; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class NetUtil { public static final String URL_BEMFA = "https://apis.bemfa.com/va/getmsg?uid=c5d604eb9c3d46d5803b4a6d0dd427bc&topic=sensors&type=3"; public static String doGet(String urlStr) { String result = ""; HttpURLConnection connection = null; InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; try { URL url = new URL(urlStr); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); bufferedReader = new BufferedReader(inputStreamReader); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line); } result = stringBuilder.toString(); } else { Log.e("NetUtil", "HTTP request failed with status code: " + responseCode); } } catch (Exception e) { e.printStackTrace(); Log.e("NetUtil", "HTTP request failed: " + e.getMessage()); } finally { if (connection != null) { connection.disconnect(); } if (inputStreamReader != null) { try { inputStreamReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } public static String doPost(String urlStr, String json) { String result = ""; HttpURLConnection connection = null; OutputStream outputStream = null; try { URL url = new URL(urlStr); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setConnectTimeout(5000); outputStream = connection.getOutputStream(); outputStream.write(json.getBytes()); outputStream.flush(); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); bufferedReader = new BufferedReader(inputStreamReader); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line); } result = stringBuilder.toString(); } else { Log.e("NetUtil", "HTTP request failed with status code: " + responseCode); } } catch (Exception e) { e.printStackTrace(); Log.e("NetUtil", "HTTP request failed: " + e.getMessage()); } finally { if (connection != null) { connection.disconnect(); } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } }httpqing求失败
06-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值