public JSONObject urlConn(String urlStr, String portStr){
String port = getPort(portStr);
urlStr = (urlStr != null)?(host + port + "/?" + urlStr):(host + port);
int responseCode;

try {
url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
responseCode = conn.getResponseCode();//获取返回码

if( responseCode == HttpURLConnection.HTTP_OK ){
is = conn.getInputStream();//获取输入流
//读取数据流
bufferedReader = new BufferedReader(new InputStreamReader(is));
//建立字符串操作对象
builder = new StringBuilder();
String line = "";
while( (line = bufferedReader.readLine()) != null ){
builder.append(line);
}

bufferedReader.close();
is.close();

//json解析
jsonObject = new JSONObject(builder.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}

return jsonObject;
}

相关文章:

  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2022-02-09
  • 2021-08-08
猜你喜欢
  • 2021-12-03
  • 2022-12-23
  • 2021-05-18
  • 2022-12-23
  • 2022-02-10
  • 2021-08-16
  • 2021-06-20
相关资源
相似解决方案