/**
* 通過okHttp3 請求網絡數據,返回String類型數據
*/
public class GetJsonData {
private static String TAG = "TESTJSON";
private static String data = "";
public static String getJsonData(String urlStr) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(urlStr)
.build();
Call call = client.newCall(request);
try {
Response response = call.execute();
data = response.body().string();
Log.i(TAG, "獲取數據成功");
Log.i(TAG, "response code =" + response.code());
Log.i(TAG, "response body = " + data);
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
}