Я не могу получить полные данные с сервера с помощью API.
Я использую AsyncTask и HttpURLConnection для получения данных, это может работать.
Но когда сервер возвращает больше данных, я обнаружил, что я не могу получить полные данные.
Как я могу это исправить, спасибо.
Вот мой код. и в этом коде я не могу получить данные jsonArray_banner
, он всегда возвращает нулевой массив.
public class JSONTask_Login extends AsyncTask<String, String, String> {
private String deviceId, os, ver;
public JSONTask_Login(String _deviceId, String _os, String _ver) {
this.deviceId = _deviceId;
this.os = _os;
this.ver = _ver;
}
@Override
protected String doInBackground(String... params) {
try {
String SERVER_WS_URL = params[0];
parameter.clear();
parameter.put("deviceId", deviceId);
parameter.put("os", os);
parameter.put("ver", ver);
return ApiCall.postWebserviceCall(SERVER_WS_URL, parameter); // POST.
} catch (Exception e) {
return null;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
if (result != null) {
JSONObject jsonObject = new JSONObject(result);
code = jsonObject.getString("code");
msg = jsonObject.getString("msg");
if (Integer.valueOf(code) == 0) {
// 1st block.
JSONObject jsonObject_userInfo = jsonObject.getJSONObject("userInfo");
....
// 2nd block.
JSONArray jsonArray_first = jsonObject.getJSONArray("anonymous_first_name");
for(int i = 0; i < jsonArray_first.length(); i++) {
JSONObject jsonObject_anonFirst = jsonArray_first.getJSONObject(i);
...
}
// 3rd block.
JSONArray jsonArray_Second = jsonObject.getJSONArray("anonymous_second_name");
for(int i = 0; i < jsonArray_Second.length(); i++) {
JSONObject jsonObject_anonSecond = jsonArray_Second.getJSONObject(i);
...
}
// 4th block.
JSONArray jsonArray_Head = jsonObject.getJSONArray("anonymous_head");
for(int i = 0; i < jsonArray_Head.length(); i++) {
JSONObject jsonObject_anonHead = jsonArray_Head.getJSONObject(i);
...
}
// 5th block.
JSONArray jsonArray_list = jsonObject.getJSONArray("posts_group_list");
for(int i = 0; i < jsonArray_list.length(); i++) {
JSONObject jsonObject_list = jsonArray_list.getJSONObject(i);
...
}
// 6th block.
// I can get data of this block, jsonArray_banner always return null array.
JSONArray jsonArray_banner = jsonObject.getJSONArray("banner_home");
for(int i = 0; i < jsonArray_banner.length(); i++) {
JSONObject jsonObject_banner = jsonArray_banner.getJSONObject(i);
...
}
}
else
return;
} else
return;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Вот мой ApiCall
код:
class ApiCall {
static String postWebserviceCall(String URL, LinkedHashMap<String, String> params) {
try {
URL url = new URL(URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(10000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
if (params != null)
writer.write(getPostDataString(params));
writer.flush();
writer.close();
os.close();
conn.connect();
int responseCode = conn.getResponseCode();
InputStream iStream;
if (responseCode == HttpURLConnection.HTTP_OK)
iStream = conn.getInputStream();
else
iStream = conn.getErrorStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuilder response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
Log.e("test", line);
}
return response.toString();
} catch (Exception e) {
return null;
}
}
static String getWebserviceCall(String URL, LinkedHashMap<String, String> params) {
...
}
private static String getPostDataString(LinkedHashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}
Вот моя структура API:
{
code: 0,
msg: "",
userInfo: {
token: "token",
nick: "user",
head: "Head/Default.png",
...
},
anonymous_first_name: [
{
id: "1",
nick_name: "one",
},
{
id: "2",
nick_name: "two",
},
...
],
anonymous_second_name: [
{
id: "1",
nick_name: "one",
},
{
id: "2",
nick_name: "two",
},
...
],
anonymous_head: [
{
id: "1",
head: "System/AnonHead/star_head_01.png",
},
{
id: "2",
head: "System/AnonHead/star_head_02.png",
},
...
],
posts_group_list: [
{
post_group: "1",
title: "title",
short_title: "stitle",
icon: "System/Classify/classify_01_food.png",
img: "",
color_code: "#E53737",
},
{
post_group: "2",
title: "title",
short_title: "stitle",
icon: "System/Classify/classify_02_travel.png",
img: "",
color_code: "#3C9BA5",
},
...
],
banner_home: [
{
title: "title",
gid: "25",
url: "url",
banner: "imgurl",
start_time: "2019-08-30 00:00:00",
end_time: "2021-08-30 00:00:00",
},
{
title: "title",
gid: "24",
url: "url",
banner: "imgurl",
start_time: "2019-08-30 00:00:00",
end_time: "2021-08-30 00:00:00",
},
...
],
}