Android Studio獲取網(wǎng)絡(luò)JSON數(shù)據(jù)并處理的方法
本文實(shí)例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下
1、需要的網(wǎng)絡(luò)JSON數(shù)據(jù)

2、數(shù)據(jù)實(shí)現(xiàn)類
package chenglong.activitytest.pengintohospital.entity;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* 科室
* Created by LICHENGLONG on 2017-10-02.
*/
public class BasSection {
public Integer id;//科室id
public String sectionName;//科室名稱
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSectionName() {
return sectionName;
}
public void setSectionName(String sectionName) {
this.sectionName = sectionName;
}
public BasSection(Integer id, String sectionName){
this.id = id;
this.sectionName = sectionName;
}
public static BasSection sectionData(JSONObject json){
try {
return new BasSection(
json.getInt("id"),
json.getString("sectionName");
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
3、創(chuàng)建List集合接收數(shù)據(jù)
List<BasSection> listBasSection = new ArrayList<>();
4、獲取JSON數(shù)據(jù)
/**
* 獲取JSON科室數(shù)據(jù)
*/
public void findSectionData(){
AsyncHttpClient client = new AsyncHttpClient();
//你的JSON數(shù)據(jù)鏈接地址
client.get(AbAppConfig.DATA_URL + "appGVConsultation/findSectionData", null, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
JSONObject object = new JSONObject(new String(responseBody));//獲取json數(shù)據(jù)
JSONArray jsonArray = object.getJSONArray("obj");//獲取數(shù)據(jù)集名稱為obj的數(shù)據(jù)
Log.d("jsonArray數(shù)據(jù)輸出:", String.valueOf(jsonArray));
for (int i = 0; i < jsonArray.length();i++) {
BasSection novels = BasSection.sectionData(jsonArray.getJSONObject(i));//把數(shù)據(jù)存在novels集合中
if (novels != null){
listBasSection.add(novels);
}
}
} catch (JSONException e) {
Toast.makeText(GV_Consultation.this, "數(shù)據(jù)請(qǐng)求失敗,請(qǐng)稍后重試", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//請(qǐng)求失敗的回調(diào)處理
Toast.makeText(GV_Consultation.this, "請(qǐng)鏈接網(wǎng)絡(luò),稍后重試", Toast.LENGTH_SHORT).show();
}
});
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android客戶端從服務(wù)器端獲取json數(shù)據(jù)并解析的實(shí)現(xiàn)代碼
- Android中使用Gson解析JSON數(shù)據(jù)的兩種方法
- Android中的JSON詳細(xì)總結(jié)
- Android網(wǎng)絡(luò)編程之獲取網(wǎng)絡(luò)上的Json數(shù)據(jù)實(shí)例
- Android webview與js交換JSON對(duì)象數(shù)據(jù)示例
- Android訪問(wèn)php取回json數(shù)據(jù)實(shí)例
- Android 解析JSON對(duì)象及實(shí)例說(shuō)明
- Android中生成、使用Json數(shù)據(jù)實(shí)例
- Android中Json數(shù)據(jù)讀取與創(chuàng)建的方法
- android調(diào)用國(guó)家氣象局天氣預(yù)報(bào)接口json數(shù)據(jù)格式解釋
相關(guān)文章
Android編程實(shí)現(xiàn)檢測(cè)當(dāng)前電源狀態(tài)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)檢測(cè)當(dāng)前電源狀態(tài)的方法,涉及Android針對(duì)當(dāng)前電源的電量、容量、伏數(shù)、溫度等的檢測(cè)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-11-11
Java操作FreeMarker模板引擎的基本用法示例小結(jié)
這篇文章主要介紹了Java操作FreeMarker模板引擎的基本用法示例小結(jié),FreeMarker本身由Java寫成,用模板來(lái)生成文本輸出,需要的朋友可以參考下2016-02-02
移動(dòng)端開發(fā)之Jetpack?Hilt技術(shù)實(shí)現(xiàn)解耦
Hilt的出現(xiàn)解決前兩點(diǎn)問(wèn)題,因?yàn)镠ilt是Dagger針對(duì)Android平臺(tái)的場(chǎng)景化框架,比如Dagger需要我們手動(dòng)聲明注入的地方,而Android聲明的地方不都在onCreate()嗎,所以Hilt就幫我們做了,除此之外還做了很多事情2023-02-02
Android 實(shí)現(xiàn)界面刷新的幾種方法
這篇文章主要介紹了Android 實(shí)現(xiàn)界面刷新的相關(guān)資料,這里提供了幾種方法及實(shí)例代碼,具有一定的參考價(jià)值,需要的朋友可以參考下2016-11-11
android點(diǎn)擊無(wú)效驗(yàn)證的解決方法
這篇文章主要給大家介紹了關(guān)于android點(diǎn)擊無(wú)效驗(yàn)證的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

