URLConnection
WebKit
JSONObject
答:使用HttpURLConnection访问网络的步骤如下:
(1) 创建URL对象。
(2) 调用URL对象的openConnection()方法获取HttpURLConnection对象。
(3) 调用setRequestMethod()方法设置http请求的方式。
(4) 通过setConnectTimeout()方法设置连接的超时时间。
(5) 调用getInputStream()方法获取服务器返回的输入流。
(6) 最后调用disconnect()方法关闭http连接。
[{“name”:“LiLi”,“score”:“95”},{“name”:“LiLei”,“score”:“99”},
{“name”:“王小明”,“score”:“100”},{“name”:“LiLei”,“score”:“89”}]
答:使用JSONArray类解析JSON数据的逻辑代码如下:
public void getJson(){
String json = "["
+ "{" +
"\"" + "name" + "\"" + ":" +
"\"" + "LiLi" + "\"" +
","+"\"" + "score" + "\"" +
":" + "\"" + "95" + "\""+
"},"
+ "{" + "\"" +
"name" + "\"" + ":" + "\"" +
"LiLei" + "\"" + ","+"\"" +
"score" + "\"" + ":" + "\"" +
"99" + "\""+ "},"
+ "{" + "\"" +
"name" + "\"" + ":" + "\"" +
"李小明" +
"\"" + ","+"\"" + "score" +
"\"" + ":" + "\"" + "100" +
"\""+ "},"
+ "{" + "\"" +
"name" + "\"" + ":" + "\"" +
"王小敏" +
"\"" + ","+"\"" + "score" +
"\"" + ":" + "\"" + "89" +
"\""+ "}"+ "]";
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(json);
for(int i = 0; i <
jsonArray.length(); i++) {
JSONObject jsonObj =
jsonArray.getJSONObject(i);
String name =
jsonObj.optString("name");
int score =
jsonObj.optInt("score");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
本文地址:https://blog.csdn.net/An_xx_/article/details/107428284
如对本文有疑问, 点击进行留言回复!!
使用纯前端JavaScript实现Excel导入导出方法过程详解
微信小程序完美解决scroll-view高度自适应问题的方法
网友评论