我正在尝试解析这个JSON。
JSON响应here
我没有在log cat中获得完整的JSON数据链接
的AsyncTask
public class FetchMangaTask extends AsyncTask<String,Void,String[]>{
private final String LOG_TAG = FetchMangaTask.class.getSimpleName();
private String[] getMangaDataFromJson(String mangaJsonStr, int pages) throws JSONException{
final String MANGA_LIST = "manga";
final String MANGA_TITLE = "t";
final String MANGA_ID = "i";
JSONObject mangaJson = new JSONObject(mangaJsonStr);
JSONArray mangaArray = mangaJson.getJSONArray(MANGA_LIST);
String[] resultStr = new String[pages];
for (int i = 0; i <mangaArray.length(); i++){
JSONObject manga = mangaArray.getJSONObject(i);
String title = manga.getString(MANGA_TITLE);
String id = manga.getString(MANGA_ID);
}
for (String s : resultStr){
Log.v(LOG_TAG,"Manga Entry" + s);
}
return resultStr;
}
@Override
protected String[] doInBackground(String... params) {
if (params.length == 0){
return null;
}
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String mangaJsonStr = null;
int pages = 1;
try {
final String MANGA_BASE_URL = "https://www.mangaeden.com/api/list/0/?";
final String PAGES_PARAM = "p";
Uri builtUri = Uri.parse(MANGA_BASE_URL).buildUpon()
.appendQueryParameter(PAGES_PARAM, Integer.toString(pages))
.build();
URL url = new URL(builtUri.toString());
Log.v(LOG_TAG, "Built URI " + builtUri.toString());
urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null){
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine())!= null){
buffer.append(line + "\n");
}
if (buffer.length() == 0){
return null;
}
mangaJsonStr = buffer.toString();
}catch (IOException e){
Log.e(LOG_TAG, "Error", e);
return null;
}finally {
if (urlConnection != null){
urlConnection.disconnect();
}
if (reader != null){
try{
reader.close();
}catch (final IOException e){
Log.e(LOG_TAG,"Error closing stream", e);
}
}
try {
return getMangaDataFromJson(mangaJsonStr, pages);
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
}
return null;
}
}
我只是在Android Studio的日志中获取此内容
记录猫
11-09 13:08:48.859 675-675/? I/art: Late-enabling -Xcheck:jni
11-09 13:08:48.891 675-691/? I/art: Debugger is no longer active
11-09 13:08:49.286 675-675/? I/LoadedApk: No resource references to update in package common
11-09 13:08:49.287 675-675/? I/LoadedApk: No resource references to update in package com.cyngn.yu
11-09 13:08:49.411 675-755/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-09 13:08:49.417 675-675/? D/Atlas: Validating map...
11-09 13:08:49.459 675-755/? I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017_msm8916_64_LA.BR.1.1.3_RB1__release_AU (Iac7c2e2837)
11-09 13:08:49.459 675-755/? I/Adreno-EGL: OpenGL ES Shader Compiler Version: E031.25.03.04
11-09 13:08:49.459 675-755/? I/Adreno-EGL: Build Date: 06/11/15 Thu
11-09 13:08:49.459 675-755/? I/Adreno-EGL: Local Branch: mybranch10882158
11-09 13:08:49.459 675-755/? I/Adreno-EGL: Remote Branch: quic/LA.BR.1.1.3_rb1.16
11-09 13:08:49.459 675-755/? I/Adreno-EGL: Local Patches: NONE
11-09 13:08:49.459 675-755/? I/Adreno-EGL: Reconstruct Branch: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017 + 26a3cba + 6f69ea6 + 8bc2bc8 + 649fcde + a52cccf + dbf281f + 15f0bf8 + 8d02f76 + 9b2cb1a + 25f3b04 + 7cd8c84 + b54906e + 675fd74 + 7c22ef4 + NOTHING
11-09 13:08:49.460 675-755/? I/OpenGLRenderer: Initialized EGL, version 1.4
11-09 13:08:49.471 675-755/? D/OpenGLRenderer: Enabling debug mode 0
11-09 13:08:49.474 675-755/? I/qdutils: PartialUpdate status: Disabled
11-09 13:08:49.474 675-755/? I/qdutils: Left Align: 0
11-09 13:08:49.474 675-755/? I/qdutils: Width Align: 0
11-09 13:08:49.474 675-755/? I/qdutils: Top Align: 0
11-09 13:08:49.474 675-755/? I/qdutils: Height Align: 0
11-09 13:08:49.474 675-755/? I/qdutils: Min ROI Width: 0
11-09 13:08:49.474 675-755/? I/qdutils: Min ROI Height: 0
11-09 13:08:49.474 675-755/? I/qdutils: Needs ROI Merge: 0
11-09 13:08:49.474 675-755/? I/qdutils: Dynamic Fps: Disabled
11-09 13:08:49.474 675-755/? I/qdutils: Min Panel fps: 0
11-09 13:08:49.474 675-755/? I/qdutils: Max Panel fps: 0
11-09 13:08:49.580 675-675/? I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@77a831a time:12438245
11-09 13:08:49.688 675-675/? I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@77a831a time:12438353
11-09 13:08:53.757 675-675/xyz.tahademure.purplereader W/art: Before Android 4.1, method int android.support.v7.internal.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
11-09 13:08:54.069 675-976/xyz.tahademure.purplereader V/FetchMangaTask: Built URI https://www.mangaeden.com/api/list/0/?p=1
11-09 13:08:55.951 675-976/xyz.tahademure.purplereader V/FetchMangaTask: Manga Entrynull
答案 0 :(得分:0)
String[] resultStr = new String[pages];
for (int i = 0; i <mangaArray.length(); i++){
JSONObject manga = mangaArray.getJSONObject(i);
String title = manga.getString(MANGA_TITLE);
String id = manga.getString(MANGA_ID);
}
for (String s : resultStr){
Log.v(LOG_TAG,"Manga Entry" + s);
}
你永远不会在resultStr中设置任何东西。我猜你想要这样的东西:
String[] resultStr = new String[pages];
for (int i = 0; i <mangaArray.length(); i++){
JSONObject manga = mangaArray.getJSONObject(i);
String title = manga.getString(MANGA_TITLE);
String id = manga.getString(MANGA_ID);
if (i < pages){
resultStr[i] = title;
}
}
for (String s : resultStr){
Log.v(LOG_TAG,"Manga Entry" + s);
}
答案 1 :(得分:0)
您永远不会在resultStr
内添加值。为什么你没有记录你的条目或在第一个循环中为resultStr
添加值?
for (int i = 0; i <mangaArray.length(); i++){
JSONObject manga = mangaArray.getJSONObject(i);
String title = manga.getString(MANGA_TITLE);
String id = manga.getString(MANGA_ID);
//Log
Log.v(LOG_TAG,"Manga Entry" + manga.toString());
//Add value assuming i < pages
resultStr[i] = manga.toString();
}
答案 2 :(得分:0)
我建议您始终使用JSON的类结构。 类页面封装了JSON中的每个页面。您还可以添加meta到JSONParser类来控制加载。
import android.net.Uri;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
/**
* @autor Sergey Shustikov (sergey.shustikov@agileengine.com)
*/
public class JSONParser {
private static final String LOG_TAG = JSONParser.class.getSimpleName();
public interface PageCallback {
void onPagesReceived(ArrayList<Page> pages);
void onFail();
}
public class Page {
public String a;
public ArrayList<String> c;
public String h;
public String id;
public String im;
public String ld;
public String s;
public String title;
}
public void loadPages(final int pageIndex, final PageCallback callback) {
new Thread(new Runnable() {
@Override
public void run() {
load(pageIndex, callback);
}
}).start();
}
private void load(int pageIndex, PageCallback callback) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String mangaJsonStr = null;
int pages = pageIndex;
try {
final String MANGA_BASE_URL = "https://www.mangaeden.com/api/list/0/?";
final String PAGES_PARAM = "p";
Uri builtUri = Uri.parse(MANGA_BASE_URL).buildUpon()
.appendQueryParameter(PAGES_PARAM, Integer.toString(pages))
.build();
URL url = new URL(builtUri.toString());
Log.v(LOG_TAG, "Built URI " + builtUri.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
mangaJsonStr = buffer.toString();
try {
ArrayList<Page> pgs = getPages(mangaJsonStr);
callback.onPagesReceived(pgs);
} catch (JSONException e) {
e.printStackTrace();
callback.onFail();
}
} catch (IOException e) {
Log.e(LOG_TAG, "Error", e);
callback.onFail();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}
}
private ArrayList<Page> getPages(String mangaJsonStr) throws JSONException {
final String MANGA_LIST = "manga";
final String MANGA_TITLE = "t";
final String MANGA_ID = "i";
JSONObject mangaJson = new JSONObject(mangaJsonStr);
JSONArray mangaArray = mangaJson.getJSONArray(MANGA_LIST);
ArrayList<Page> pages = new ArrayList<>();
for (int i = 0; i < mangaArray.length(); i++) {
JSONObject manga = mangaArray.getJSONObject(i);
String title = manga.getString(MANGA_TITLE);
String id = manga.getString(MANGA_ID);
Page page = new Page();
page.title = title;
page.id = id;
pages.add(page);
}
return pages;
}
}
加载:
JSONParser parser = new JSONParser();
parser.loadPages(1, new JSONParser.PageCallback() {
@Override
public void onPagesReceived(ArrayList<JSONParser.Page> pages) {
System.out.println(pages.size());
}
@Override
public void onFail() {
}
});