java 原生json解析_json原生解析

该博客介绍了如何在Java中使用原生JSON解析技术处理从网络获取的数据。在`ChaXunJieGuoActivity`类中,通过OkHttp发送网络请求,获取JSON响应,然后使用`JSONArray`和`JSONObject`进行解析。解析后的数据用于填充列表视图展示查询结果。当点击列表项时,会跳转到相应的详情页面。
摘要由CSDN通过智能技术生成

packagecom.tfot.hotel.yichengyiyu.Activity.zhou_activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.support.annotation.Nullable;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ImageView;importandroid.widget.ListView;importcom.squareup.okhttp.Call;importcom.squareup.okhttp.FormEncodingBuilder;importcom.squareup.okhttp.OkHttpClient;importcom.squareup.okhttp.Request;importcom.squareup.okhttp.RequestBody;importcom.squareup.okhttp.Response;importcom.tfot.hotel.yichengyiyu.Activity.NewActivivty.XiangQingActivity;importcom.tfot.hotel.yichengyiyu.Activity.NewActivivty.XiangQingActivity_Long;importcom.tfot.hotel.yichengyiyu.Activity.zhou_activity.adapter.ChaXunJieGuoAdapter;importcom.tfot.hotel.yichengyiyu.Activity.zhou_activity.bean.ChaXunJieGuo;importcom.tfot.hotel.yichengyiyu.R;importcom.tfot.hotel.yichengyiyu.Util.Common;importcom.tfot.hotel.yichengyiyu.Util.base.BaseActivity;importorg.json.JSONArray;importorg.json.JSONException;importorg.json.JSONObject;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importrx.Observable;importrx.Observer;importrx.Subscriber;importrx.Subscription;importrx.android.schedulers.AndroidSchedulers;importrx.schedulers.Schedulers;importrx.subscriptions.CompositeSubscription;/*** 查询结果界面

* Created by huizhou on 2017/6/26.*/

public class ChaXunJieGuoActivity extendsBaseActivity {private List chaXunJieGuoList=new ArrayList<>(); //查询结果数据源

private String changOrduang ;//CONDITION 判断长租或者短租的字符串

private Boolean yeOrNo = false ;//查询内容是否存在

privateString sousuoneiron;privateListView activity_chaxunjieguo_list;privateImageView activivty_chaxunjieguo_fanhuui;privateChaXunJieGuoAdapter chaXunJieGuoAdapter;privateView kongbaiye_sousuo;private CompositeSubscription mCompositeSubscription;//第三方线程

private final OkHttpClient client = newOkHttpClient();

@Overrideprotected voidonCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_chaxunjieguo_l);

Intent intent=getIntent();

Bundle bundle=intent.getExtras();//.getExtras()得到intent所附带的额外数据

changOrduang = bundle.getString("changOrduang");

sousuoneiron= bundle.getString("sousuojieguoneiron");

initData();

}private voidinitData() {

mCompositeSubscription= new CompositeSubscription(); //第三方线程

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

activivty_chaxunjieguo_fanhuui=(ImageView) findViewById(R.id.activivty_chaxunjieguo_fanhuui);

kongbaiye_sousuo= findViewById(R.id.kongbaiye_sousuo); //空白页

chaxunjieguo(sousuoneiron);

chaXunJieGuoAdapter= new ChaXunJieGuoAdapter(ChaXunJieGuoActivity.this,chaXunJieGuoList);

activity_chaxunjieguo_list.setAdapter(chaXunJieGuoAdapter);

activity_chaxunjieguo_list.setOnItemClickListener(newAdapterView.OnItemClickListener() {

@Overridepublic void onItemClick(AdapterView> parent, View view, int position, longid) {//TODO 查询条目跳转

Intent intent =null;if(changOrduang.equals("day")){

intent= new Intent(ChaXunJieGuoActivity.this, XiangQingActivity.class); //短租的房间详情页

}else if(changOrduang.equals("long")){

intent= new Intent(ChaXunJieGuoActivity.this, XiangQingActivity_Long.class); //长租的房间详情页

}

Bundle bundle= newBundle();

bundle.putString("RoomStyleId",chaXunJieGuoList.get(position).getRoomStyleId());

bundle.putString("Brandname",chaXunJieGuoList.get(position).getBrandname());

intent.putExtras(bundle);

startActivity(intent);

}

});

activivty_chaxunjieguo_fanhuui.setOnClickListener(newView.OnClickListener() {

@Overridepublic voidonClick(View v) {

finish();

}

});

}//查询结果的的网路连接

private void chaxunjieguo(finalString chaxuneironB){

Subscription subscription= Observable.create(new Observable.OnSubscribe() {

@Overridepublic void call(Subscriber super String>subscriber) {

RequestBody formBody= newFormEncodingBuilder()

.add("ACT", "likesearch")

.add("CONDITION",changOrduang)

.add("content",chaxuneironB)

.build();final Request request = newRequest.Builder()

.url(Common.NEW_CHAXUNJIEGUO)

.post(formBody)

.build();

Call call=client.newCall(request);try{

Response response=call.execute();

subscriber.onNext(response.body().string());

subscriber.onCompleted();

}catch(IOException e) {

e.printStackTrace();

}

}

}).subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(new Observer() {

@Overridepublic voidonCompleted() {

}

@Overridepublic voidonError(Throwable e) {

}

@Overridepublic voidonNext(String s) {//原生解析

try{

JSONArray array= newJSONArray(s);

chaXunJieGuoList.clear();for (int i = 0; i < array.length(); i++) {

JSONObject temp=(JSONObject) array.get(i);

String RoomStyleId=temp.getString("RoomStyleId");

String Unitprice=temp.getString("Unitprice");

String evaluate=temp.getString("evaluate");

JSONObject banner=temp.getJSONObject("Brand");

String Brandname=banner.getString("Brandname");

JSONArray jsonArray=temp.getJSONArray("RoomStyleImage");

ArrayList RoomStyleImage=new ArrayList();for (int i2=0;i2

String a= Common.BASIC_IC_API+jsonArray.get(i).toString();

RoomStyleImage.add(a);

}

String RoomStyleAddres=temp.getString("RoomStyleAddres");

String Roomlongrentom=temp.getString("Roomlongrentom");

String RoomStyleName= temp.getString("RoomStyleName");

String RoomStar= temp.getString("RoomStar");

ChaXunJieGuo chaXunJieGuoData= newChaXunJieGuo(RoomStyleId,Unitprice,

evaluate, Brandname,RoomStyleImage,RoomStyleAddres,Roomlongrentom,RoomStyleName,RoomStar);

chaXunJieGuoList.add(chaXunJieGuoData);

}if(!chaXunJieGuoList.isEmpty()){

kongbaiye_sousuo.setVisibility(View.GONE);

activity_chaxunjieguo_list.setVisibility(View.VISIBLE);

}else{

kongbaiye_sousuo.setVisibility(View.VISIBLE);

activity_chaxunjieguo_list.setVisibility(View.GONE);

}

}catch(JSONException e) {

e.printStackTrace();

}

chaXunJieGuoAdapter.notifyDataSetChanged();

}

});

mCompositeSubscription.add(subscription);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值