为了账号安全,请及时绑定邮箱和手机立即绑定

使用httpurlconnection设置http标头

使用httpurlconnection设置http标头

PIPIONE 2021-05-15 18:14:41
我在设置http请求的标头时遇到问题,这是我正在使用的代码    public static JSONObject getJSONObjectFromURL(String urlString, String version, String menuTop, String menuBottom) throws IOException, JSONException {    HttpURLConnection urlConnection = null;    URL url = new URL(urlString);    urlConnection = (HttpURLConnection) url.openConnection();    urlConnection.setRequestMethod("GET");    urlConnection.setRequestProperty("X-Mobile-Version", MessageFormat.format("android/{0}", version));    urlConnection.setRequestProperty("X-Menu-Hash", menuTop);    urlConnection.setRequestProperty("X-MenuBottom-Hash", menuBottom);    urlConnection.setReadTimeout(10000 /* milliseconds */ );    urlConnection.setConnectTimeout(15000 /* milliseconds */ );    urlConnection.setDoOutput(true);    urlConnection.connect();    //Log.d("-------------------....",urlConnection.getRequestProperty("X-Mobile-Version"));    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));    StringBuilder sb = new StringBuilder();    String line;    while ((line = br.readLine()) != null) {        sb.append(line + "\n");    }    br.close();    String jsonString = sb.toString();    Log.d("..........JSon", jsonString);    return new JSONObject(jsonString);}但我总是得到以下错误W/System.err: java.io.FileNotFoundException: endpoint/W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)              at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)              at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(Unknown Source:0)              at java.net.URL.openStream(URL.java:1058)              at com.example.bordi.hotel.DataManager.getJSONObjectFromURL(DataManager.java:109)              at com.example.bordi.hotel.DataManager$1.run(DataManager.java:132)              at java.lang.Thread.run(Thread.java:764)我该如何解决?提前致谢!
查看完整描述

1 回答

?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

解决方法如下:

 BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));



查看完整回答
反对 回复 2021-05-19
  • 1 回答
  • 0 关注
  • 321 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信