2013-03-30 40 views
2

我正在嘗試獲取JSON,但我必須在AsyncTask中執行此操作,因爲我在logcat AndroidRuntime(18153): Caused by: android.os.NetworkOnMainThreadException中獲取了此內容。在AsyncTask中獲取JSON Android

這裏是我的代碼:

public class LatestAlbums extends Activity { 

    TextView t; 

    // url to make request 
    private static String url = "www.example.com"; 

    // JSON Node names 
    private static final String TAG_ALBUMS = "albums"; 
    private static final String TAG_ID = "id"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_SINGER = "singer"; 
    private static final String TAG_GENRE = "genre"; 
    private static final String TAG_MIX = "mix"; 
    private static final String TAG_THUMB = "thumb"; 
    private static final String TAG_SONGS = "songs"; 
    private static final String TAG_SONG_TITLE = "song"; 
    private static final String TAG_SONG_ARTIST = "artist"; 
    private static final String TAG_SONG_MP3 = "mp3"; 
    private static final String TAG_SONG_MP4 = "mp4"; 
    private static final String TAG_SONG_THUMB = "thumb"; 

    // albums JSONArray 
    JSONArray albums = null; 
    JSONArray sngs = null; 
    JSONObject objects = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.favorites); 
     t = (TextView) findViewById(R.id.json); 
     loadJSON(); 
    } 

    public void loadJSON() { 
     // Creating JSON Parser instance 
     JSONParser jParser = new JSONParser(); 

     // getting JSON string from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 

     try { 
      // Getting Array of albums 

      albums = json.getJSONArray(TAG_ALBUMS); 
      sngs=json.getJSONArray(TAG_SONGS); 
      // looping through All albums 
      for (int i = 0; i < albums.length(); i++) { 
       JSONObject c = albums.getJSONObject(i); 

       // Storing each json item in variable 
       String album_id = c.getString(TAG_ID); 
       String album_name = c.getString(TAG_NAME); 
       String album_singer = c.getString(TAG_SINGER); 
       String album_genre = c.getString(TAG_GENRE); 
       String album_thumb = c.getString(TAG_THUMB); 

       // songs are again JSON Object 
       for (int j = 0; i < sngs.length(); j++) { 
        JSONObject s = sngs.getJSONObject(j); 
        JSONObject songs = s.getJSONObject(TAG_SONGS); 
        String artist = s.getString(TAG_SONG_ARTIST); 
        String mp3 = s.getString(TAG_SONG_MP3); 
        String mp4 = s.getString(TAG_SONG_MP4); 
        String song_thumb = s.getString(TAG_SONG_THUMB); 
        String song_title = s.getString(TAG_SONG_TITLE); 
       } 
       Log.v("--", "Albums \n" + " " + album_id + " " + album_name 
         + " " + album_genre + " " + album_singer + " " 
         + album_thumb); 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 

} 

可有人請告訴我如何做到這一點?

回答

4

加載在後臺的JSON對象(做網絡操作)和處理導致UI線程,即:

URL requestUrl = "..."; 

new AsyncTask<URL, Void, JSONObject>() { 

    @Override 
    protected Boolean doInBackground(URL... urls) { 
     loadJSON(url); 
    } 

    @Override 
    protected void onPostExecute(JSONObject jsonData) { 
     try { 
      // Getting Array of albums 

      albums = json.getJSONArray(TAG_ALBUMS); 
      sngs=json.getJSONArray(TAG_SONGS); 
      // looping through All albums 

      etc. 
    } 
}.execute(requestUrl); 


public void loadJSON(URL url) { 
    // Creating JSON Parser instance 
    JSONParser jParser = new JSONParser(); 

    // getting JSON string from URL 
    JSONObject json = jParser.getJSONFromUrl(url); 

    return json; 
} 

查看this瞭解更多信息。

乾杯!

+0

當我把它放在我的代碼中時,我得到了很多錯誤。我是否必須把這個放在一個新班級 –

+1

我只是想給你一個粗略的想法。當然,還有一些事情要做。但這個想法應該清楚,不是嗎?附:如果你遇到困難,粘貼你的更新代碼並添加什麼不起作用 – Trinimon

+1

@BozidarPrcovski你有沒有在developers.android.com上看過'ASyncTask'?如果是這樣的話,在實施時不應該有任何問題。 –

10

最簡單的方法來讀取JSON並將其設置爲適配器。

AsyncTask有三種方法。

PreExecute and PostExecute

你可以設置任何視圖屬性(如你在主UI線程)doInBackground(操作比UI放在這裏交互等),它讀取JSON

package com.example.tabs; 

import java.util.ArrayList; 
import java.util.HashMap; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 
import android.widget.Toast; 

import com.example.adapters.CalendarAdapter; 
import com.example.description.CalendarDescription; 

public class Calendar extends Activity { 

    String url = "Enter your URL here "; 

    GetData data; 

    ProgressDialog progressDialog; 

    ListView list_of_calendar; 

    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.calendar); 

     list_of_calendar = (ListView) findViewById(R.id.list_of_calendar); 

     new GetData().execute(); 
     //new GetData(url).execute();//you can pass it like this and see comment in doInBackground 

     //ListView listView = getListView(); 
     list_of_calendar.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) 
      { 
       HashMap<String, String> map = list.get(position);    

       Intent intent = new Intent(Calendar.this, CalendarDescription.class); 

       intent.putExtra("name", map.get("name")); 

       intent.putExtra("date", map.get("date")); 

       intent.putExtra("description", map.get("description")); 

       startActivity(intent); 


      } 

     }); 
    } 

    private class GetData extends AsyncTask<String, Void, JSONObject> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

      progressDialog = ProgressDialog.show(Calendar.this, 
        "", ""); 

     } 

     @Override 
     protected JSONObject doInBackground(String... params) { 

      String response; 

      try { 

       HttpClient httpclient = new DefaultHttpClient(); 

       HttpPost httppost = new HttpPost(url); 
       //HttpPost httppost = new HttpPost(params[0]);//you can also pass it and get the Url here. 

       HttpResponse responce = httpclient.execute(httppost); 

       HttpEntity httpEntity = responce.getEntity(); 

       response = EntityUtils.toString(httpEntity); 

       Log.d("response is", response); 

       return new JSONObject(response); 

      } catch (Exception ex) { 

       ex.printStackTrace(); 

      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(JSONObject result) 
     { 
      super.onPostExecute(result); 

      progressDialog.dismiss(); 

      if(result != null) 
      { 
       try 
       { 
        JSONObject jobj = result.getJSONObject("result"); 

        String status = jobj.getString("status"); 

        if(status.equals("true")) 
        { 
         JSONArray array = jobj.getJSONArray("data"); 

         for(int x = 0; x < array.length(); x++) 
         { 
          HashMap<String, String> map = new HashMap<String, String>(); 

          map.put("name", array.getJSONObject(x).getString("name")); 

          map.put("date", array.getJSONObject(x).getString("date")); 

          map.put("description", array.getJSONObject(x).getString("description")); 

          list.add(map); 
         } 

         CalendarAdapter adapter = new CalendarAdapter(Calendar.this, list); 

         list_of_calendar.setAdapter(adapter); 
        } 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
      else 
      { 
       Toast.makeText(Calendar.this, "Network Problem", Toast.LENGTH_LONG).show(); 
      } 
     } 

    } 
} 

編輯

代碼的URL

http://www.socialgeolocate.com/DayCare/webservices/Get_calender.php

+0

這將是一個解析json結構的例子嗎?我不明白「結果」,「狀態」和「數據」字符串。謝謝。 –

+0

卡洛看到我更新的答案。我附上了我從哪裏獲取數據的鏈接。 – Nepster

+1

謝謝nepster,我對如何獲得一個乾淨的json數組(我的意思是,沒有dictionariess或JSONObjects之前)的疑問,但知道我知道我應該在整個asyncTask而不是JSONObject中使用JSONArray,這就是爲什麼我想看到JSON的閱讀,再次感謝.. –