在AsyncTask Android中获取JSON

在AsyncTask Android中获取JSON,第1张

概述我正在尝试获取JSON,但我必须在AsyncTask中执行此 *** 作,因为我在logcat中得到了这个AndroidRuntime(18153):引起:android.os.NetworkOnMainThreadException.这是我的代码:publicclassLatestAlbumsextendsActivity{TextViewt;//urltomakerequestprivates

我正在尝试获取JSON,但我必须在AsyncTask中执行此 *** 作,因为我在logcat中得到了这个AndroidRuntime(18153):引起:androID.os.networkonmainthreadException.

这是我的代码:

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

谁能告诉我怎么做?

解决方法:

在后台加载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以获取更多信息.

干杯!

总结

以上是内存溢出为你收集整理的在AsyncTask Android中获取JSON全部内容,希望文章能够帮你解决在AsyncTask Android中获取JSON所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1119932.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存