获取JSON在AsyncTask的机器人

获取JSON在AsyncTask的机器人

本文介绍了获取JSON在AsyncTask的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让JSON,但我必须这样做,在AsyncTask的,因为我得到这个logcat中 AndroidRuntime(18153):产生的原因:android.os.NetworkOnMainThreadException

I'm trying to get JSON but I have to do it in AsyncTask , because I get this in logcat AndroidRuntime(18153): Caused by: android.os.NetworkOnMainThreadException.

下面是我的code:

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

    }

}

谁能告诉我如何做到这一点?

Can anybody please tell me how to do this?

推荐答案

加载在后台的JSON对象(做网络操作)​​和处理结果在UI线程,即:

Load your JSON object in background (doing the network operation) and process the result in the UI thread, i.e.:

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

查看这个了解详情。

干杯!

这篇关于获取JSON在AsyncTask的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:15