前言
调用网络接口API并处理返回结果(json)。
代码
修改AndroidManifest.xml文件添加联网权限 :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.xxx">
<uses-permission android:name="android.permission.INTERNET" />
<application
......
</application>
</manifest>
”调用网络接口API“工具类 :
public class HttpUtil {
// 获取网络数据
public static String getJSON(String path) {
String json = "";
try {
// 将数据转为url对象
URL url = new URL(path);
// 获取网络连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 开始连接
conn.connect();
// 读取输入流内容
InputStream is = conn.getInputStream();
// 读取流
int hasRead =