通过url下载Json
作者:互联网
网络权限
<uses-permission android:name="android.permission.INTERNET" />
1、方式1 - okHttp
依赖
//okHttp
implementation 'com.squareup.okhttp3:okhttp:3.4.1'
接口
public interface GetJsonDataCallback {
void getJsonDataSuccess(String jsonData);
void getJsonDataFailed(String errMsg);
}
使用
/**
* 获取 json 数据
*
* @param url 请求地址
* @param callback 结果回调
*/
public void getJsonDataByOkHttp(String url, final GetJsonDataCallback callback) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callback.getJsonDataFailed(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream inputStream = response.body().byteStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
callback.getJsonDataSuccess(stringBuilder.toString());
}
});
}
2、方法2 - URLConnection、HttpURLConnection
依赖
//URLConnection、HttpURLConnection
implementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
方法定义
public class GetJsonUtils {
/**
* 获取 json 数据 - URLConnection
*
* @param jsonUrl 请求地址
* @return 结果数据
*/
public static String getJsonDataByURLConnection(String jsonUrl) {
StringBuilder resultData = new StringBuilder();
try {
URL url = new URL(jsonUrl);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
resultData.append(line);
}
} catch (Exception e) {
e.printStackTrace();
resultData.append(e.getMessage());
}
return resultData.toString();
}
/**
* 获取 json 数据 - HttpURLConnection
*
* @param jsonUrl
* @return
*/
public static String getJsonDataByHttpURLConnection(String jsonUrl) {
StringBuilder resultData = new StringBuilder();
try {
URL url = new URL(jsonUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(60 * 1000);
connection.setRequestMethod("POST");
connection.connect();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
resultData.append(line);
}
}
} catch (Exception e) {
e.printStackTrace();
resultData.append(e.getMessage());
}
return resultData.toString();
}
}
异步线程
public class GetJsonTask extends AsyncTask<String, Void, String> {
/**
* 接收输入参数、执行任务中的耗时操作、返回 线程任务执行的结果
*
* @param urls
* @return
*/
@Override
protected String doInBackground(String... urls) {
return GetJsonUtils.getJsonDataByURLConnection(urls[0]);
}
/**
* 接收线程任务执行结果、将执行结果显示到UI组件
*
* @param result
*/
@Override
protected void onPostExecute(String result) {
Log.i("TAG_ZLZ", "------ result : " + result);
}
}
使用
public void getJsonClick(View view) {
new GetJsonTask().execute(JSON_URL);
}
标签:String,url,resultData,Json,new,line,public,下载 来源: https://blog.csdn.net/weixin_41733225/article/details/117814690