【发布时间】:2018-10-31 12:53:36
【问题描述】:
有人可以帮我从网上获取 json。在函数的末尾,jsonResponse 是空的。我用这个方法来做:
private String getJson() {
jsonResponsce = "";
AsyncTask.execute(new Runnable() {
@Override
public void run() {
try{
URL httpbinEndpoint = new URL(webPage);
HttpsURLConnection myConnection = (HttpsURLConnection) httpbinEndpoint.openConnection();
myConnection.setRequestMethod("GET");
// Enable writing
myConnection.setDoOutput(true);
String internetData = "";
// Write the data
myConnection.getOutputStream().write(internetData.getBytes());
jsonResponsce = internetData;
} catch (IOException e) {
e.printStackTrace();
}
}
});
return jsonResponsce;
}
我为清单设置了 Internet 权限。我尝试从下一个地址获取 Json:https://shori-dodjo-mobile-app.firebaseio.com/.json。完整代码放在这里:https://github.com/GenkoKaradimov/Shori-Dodjo-Android-App/
【问题讨论】: