2015-11-09 72 views
0

我想解析這個JSON。如何解析Android中的JSON文檔?

JSON響應here

我不是在日誌貓只是鏈接獲取完整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: [email protected] time:12438245 
11-09 13:08:49.688 675-675/? I/Timeline: Timeline: Activity_idle id: [email protected] 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

要獲得所有的JSON字符串在日誌使用'Log.i( 「TAG」,mangaJsonStr);' –

+0

看起來你在登錄貓獲得更多。 –

+0

發佈完整日誌。 –

回答

0

我建議你總是讓你的JSON的類結構。 類頁面在您的JSON中封裝了每個頁面。您也可以添加元素到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 ([email protected]) 
*/ 
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() { 

      } 
     }); 
+0

你是最棒的! – Unkown

+0

我可以在哪裏發佈JSONParser代碼來加載? – Unkown

+0

@TahaDemure當你想加載。但我需要注意:'onPagesReceived'將在後臺線程中調用,所以如果你想更新你的UI,使用'post'或'runOnUIThread' methanism。 –

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); 
    } 
0

你從來沒有把一個值放在​​裏面。爲什麼你沒有在第一個循環中記錄您的輸入或將值添加到​​?

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(); 
} 
+0

非常感謝你! – Unkown