package com.trotri.android.rxjavasample;
public class BookData {
private int errNo;
private String errMsg;
private DataBean data;
public int getErrNo() {
return errNo;
}
public void setErrNo(int errNo) {
this.errNo = errNo;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public static class DataBean {
private int id;
private String title;
private String type;
private String description;
private String picture;
private boolean isRecommend;
private String dtCreated;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public boolean isIsRecommend() {
return isRecommend;
}
public void setIsRecommend(boolean isRecommend) {
this.isRecommend = isRecommend;
}
public String getDtCreated() {
return dtCreated;
}
public void setDtCreated(String dtCreated) {
this.dtCreated = dtCreated;
}
}
}
======================================
package com.trotri.android.rxjavasample; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import rx.Observable; import rx.Observer; import rx.Subscriber; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; public class MainActivity extends AppCompatActivity { /** * 请求的URL */ public static final String URL = "http://huixinguiyu.cn/Assets/js/data.js"; private TextView mTvTitle; private TextView mTvDtCreated; private TextView mTvDescription; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTvTitle = (TextView) findViewById(R.id.tv_title); mTvDtCreated = (TextView) findViewById(R.id.tv_dt_created); mTvDescription = (TextView) findViewById(R.id.tv_description); // 被观察者 -- 实现HTTP请求 -- 子线程 Observable.OnSubscribe<String> observable = new Observable.OnSubscribe<String>() { @Override public void call(final Subscriber<? super String> subscriber) { Request req = new Request.Builder().url(URL).build(); OkHttpClient client = new OkHttpClient(); Call call = client.newCall(req); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { subscriber.onError(e); } @Override public void onResponse(Call call, Response response) throws IOException { subscriber.onNext(response.body().string()); } }); } }; // 观察者 -- 渲染页面 -- 主线程 Observer<String> observer = new Observer<String>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Toast.makeText(MainActivity.this, "HTTP请求失败", Toast.LENGTH_LONG).show(); } @Override public void onNext(String s) { BookData data = new Gson().fromJson(s, BookData.class); mTvTitle.setText(data.getData().getTitle()); mTvDtCreated.setText(data.getData().getDtCreated()); mTvDescription.setText(data.getData().getDescription()); } }; Observable.create(observable).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(observer); } }==========================
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="16dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center_vertical" android:text="书名" android:textSize="24sp" /> <TextView android:id="@+id/tv_dt_created" android:layout_width="match_parent" android:layout_height="32dp" android:gravity="center_vertical" android:text="上架时间" android:textSize="14sp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/background_dark" /> <TextView android:id="@+id/tv_description" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="16dp" android:text="描述" android:textSize="24sp" /> </LinearLayout>========================================
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'io.reactivex:rxjava:1.0.10' compile 'io.reactivex:rxandroid:1.2.0' compile 'com.squareup.okhttp3:okhttp:3.3.0' compile 'com.google.code.gson:gson:2.7' }