当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 《Android移动应用基础教程》(Android Studio)(第二版)黑马教程 课后题答案 第11章

《Android移动应用基础教程》(Android Studio)(第二版)黑马教程 课后题答案 第11章

2020年07月23日  | 移动技术网IT编程  | 我要评论

《Android移动应用基础教程》(Android Studio)(第二版)黑马教程 课后题答案 第11章——网络与数据处理

一、填空题

  1. URLConnection

  2. WebKit

  3. JSONObject

二、判断题

  1. 对 2、对 3、对 4、对 5、对 6、错

三、选择题

  1. ABCD 2、ABD

四、简答题

  1. 简述使用HttpURLConnection 访问网络的步骤。

答:使用HttpURLConnection访问网络的步骤如下:

(1) 创建URL对象。

(2) 调用URL对象的openConnection()方法获取HttpURLConnection对象。

(3) 调用setRequestMethod()方法设置http请求的方式。

(4) 通过setConnectTimeout()方法设置连接的超时时间。

(5) 调用getInputStream()方法获取服务器返回的输入流。

(6) 最后调用disconnect()方法关闭http连接。

五、编程题

  1. 请写出使用JSONArray类解析JSON数据的主要逻辑代码。JSON数据如下所示。

[{“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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网