Skip to content

Commit

Permalink
Rx基本功能使用完成
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonlzy committed Sep 30, 2016
1 parent 8f8cce6 commit b4ebbea
Show file tree
Hide file tree
Showing 21 changed files with 835 additions and 149 deletions.
45 changes: 25 additions & 20 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,58 @@
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<application
android:name="com.lzy.demo.GApp"
android:name=".GApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.lzy.demo.MainActivity"
android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".okgo.MethodActivity"/>
<activity android:name=".okgo.JsonRequestActivity"/>
<activity android:name=".okgo.BitmapRequestActivity"/>
<activity android:name=".okgo.PostTextActivity"/>
<activity android:name=".okgo.FormUploadActivity"/>
<activity android:name=".okgo.FileDownloadActivity"/>
<activity android:name=".okgo.CacheActivity"/>
<activity android:name=".okgo.HttpsActivity"/>
<activity android:name=".okgo.SyncActivity"/>
<activity android:name=".okgo.RedirectActivity"/>
<activity android:name=".okgo.TestActivity"/>

<activity android:name="com.lzy.demo.okgo.MethodActivity"/>
<activity android:name="com.lzy.demo.okgo.JsonRequestActivity"/>
<activity android:name="com.lzy.demo.okgo.BitmapRequestActivity"/>
<activity android:name="com.lzy.demo.okgo.PostTextActivity"/>
<activity android:name="com.lzy.demo.okgo.FormUploadActivity"/>
<activity android:name="com.lzy.demo.okgo.FileDownloadActivity"/>
<activity android:name="com.lzy.demo.okgo.CacheActivity"/>
<activity android:name="com.lzy.demo.okgo.HttpsActivity"/>
<activity android:name="com.lzy.demo.okgo.SyncActivity"/>
<activity android:name="com.lzy.demo.okgo.RedirectActivity"/>
<activity android:name="com.lzy.demo.okgo.TestActivity"/>
<activity
android:name="com.lzy.demo.cache.CacheDemoActivity"
android:name=".cache.CacheDemoActivity"
android:theme="@style/AppTheme.NoActionBar"/>
<activity
android:name="com.lzy.demo.WebActivity"
android:name=".WebActivity"
android:theme="@style/AppTheme.NoActionBar"/>
<activity
android:name="com.lzy.demo.okgo.OkHttpActivity"
android:name=".okgo.OkHttpActivity"
android:theme="@style/AppTheme.NoActionBar"/>
<activity
android:name="com.lzy.demo.okserver.DownloadManagerActivity"
android:name=".okserver.DownloadManagerActivity"
android:theme="@style/AppTheme.NoActionBar"/>
<activity
android:name="com.lzy.demo.okserver.DesActivity"
android:name=".okserver.DesActivity"
android:theme="@style/AppTheme.NoActionBar"/>
<activity
android:name="com.lzy.demo.okserver.DownloadActivity"
android:name=".okserver.DownloadActivity"
android:theme="@style/AppTheme.NoActionBar"/>
<activity
android:name="com.lzy.demo.okserver.UploadActivity"
android:name=".okserver.UploadActivity"
android:theme="@style/AppTheme.NoActionBar"/>

<activity android:name=".okrx.RxCommonActivity"/>
<activity
android:name=".okrx.RxActivity"
android:theme="@style/AppTheme.NoActionBar"/>
</application>

Expand Down
111 changes: 65 additions & 46 deletions app/src/main/java/com/lzy/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import com.lzy.demo.base.BaseRecyclerAdapter;
import com.lzy.demo.base.DividerItemDecoration;
import com.lzy.demo.cache.CacheDemoActivity;
import com.lzy.demo.okserver.DownloadActivity;
import com.lzy.demo.okserver.UploadActivity;
import com.lzy.demo.okgo.FileDownloadActivity;
import com.lzy.demo.okgo.FormUploadActivity;
import com.lzy.demo.okgo.JsonRequestActivity;
import com.lzy.demo.okgo.OkHttpActivity;
import com.lzy.demo.okrx.RxActivity;
import com.lzy.demo.okrx.RxCommonActivity;
import com.lzy.demo.okserver.DownloadActivity;
import com.lzy.demo.okserver.UploadActivity;

import java.util.ArrayList;

Expand Down Expand Up @@ -53,76 +55,92 @@ public void fab(View view) {

private void initData() {
items = new ArrayList<>();

OkHttpModel model1 = new OkHttpModel();
model1.title = "";
model1.des = "下面是OkGo包的使用方法";
model1.des = "OkRx扩展2.0震撼来袭";
model1.type = 1;
items.add(model1);

OkHttpModel model2 = new OkHttpModel();
model2.title = "基本功能(OkGo)";
model2.des = "1.GET,HEAD,OPTIONS,POST,PUT,DELETE 请求方法演示\n" +
"2.请求服务器返回bitmap对象\n" +
"3.支持https请求\n" +
"4.支持同步请求\n" +
"5.支持301重定向";
model2.title = "OkRx使用方法示例";
model2.des = "1.完美结合RxJava\n" +
"2.比Retrofit更简单方便\n" +
"3.网络请求和RxJava调用,一条链点到底\n" +
"4.支持Json数据的自动解析转换";
model2.type = 0;
items.add(model2);

OkHttpModel model3 = new OkHttpModel();
model3.title = "自动解析Json对象";
model3.des = "1.自动解析JavaBean对象\n" + //
"2.自动解析List<JavaBean>集合对象";
model3.type = 0;
model3.title = "";
model3.des = "下面是OkGo包的使用方法";
model3.type = 1;
items.add(model3);

OkHttpModel model4 = new OkHttpModel();
model4.title = "文件下载";
model4.des = "1.支持大文件或小文件下载,无论多大文件都不会发生OOM\n" +
"2.支持监听下载进度和下载网速\n" +
"3.支持自定义下载目录和下载文件名";
model4.title = "基本功能(OkGo)";
model4.des = "1.GET,HEAD,OPTIONS,POST,PUT,DELETE 请求方法演示\n" +
"2.请求服务器返回bitmap对象\n" +
"3.支持https请求\n" +
"4.支持同步请求\n" +
"5.支持301重定向";
model4.type = 0;
items.add(model4);

OkHttpModel model5 = new OkHttpModel();
model5.title = "文件上传";
model5.des = "1.支持上传单个文件\n" +
"2.支持同时上传多个文件\n" +
"3.支持多个文件多个参数同时上传\n" +
"4.支持大文件上传,无论多大都不会发生OOM\n" +
"5.支持监听上传进度和上传网速";
model5.title = "自动解析Json对象";
model5.des = "1.自动解析JavaBean对象\n" + //
"2.自动解析List<JavaBean>集合对象";
model5.type = 0;
items.add(model5);

OkHttpModel model6 = new OkHttpModel();
model6.title = "强大的缓存示例 -- 先联网获取数据,然后断开网络再进试试";
model6.des = "1.OkGo的强大的缓存功能,让你代码无需关心数据来源,专注于业务逻辑的实现\n" +
"2.共有五种缓存模式满足你各种使用场景\n" +
"3.支持自定义缓存过期时间";
model6.title = "文件下载";
model6.des = "1.支持大文件或小文件下载,无论多大文件都不会发生OOM\n" +
"2.支持监听下载进度和下载网速\n" +
"3.支持自定义下载目录和下载文件名";
model6.type = 0;
items.add(model6);

OkHttpModel model7 = new OkHttpModel();
model7.title = "";
model7.des = "下面是OkServer包的使用方法";
model7.type = 1;
model7.title = "文件上传";
model7.des = "1.支持上传单个文件\n" +
"2.支持同时上传多个文件\n" +
"3.支持多个文件多个参数同时上传\n" +
"4.支持大文件上传,无论多大都不会发生OOM\n" +
"5.支持监听上传进度和上传网速";
model7.type = 0;
items.add(model7);

OkHttpModel model8 = new OkHttpModel();
model8.title = "下载管理(OkServer)";
model8.des = "1.这个属于OkServer依赖中的功能,并不属于OkGo\n" +
"2.这个包维护较少,一般情况下,不做特殊的下载管理功能,OkGo完全可以胜任\n" +
"3.相比OkGo主要是多了断点下载和下载状态的管理";
model8.title = "强大的缓存示例 -- 先联网获取数据,然后断开网络再进试试";
model8.des = "1.OkGo的强大的缓存功能,让你代码无需关心数据来源,专注于业务逻辑的实现\n" +
"2.共有五种缓存模式满足你各种使用场景\n" +
"3.支持自定义缓存过期时间";
model8.type = 0;
items.add(model8);

OkHttpModel model9 = new OkHttpModel();
model9.title = "上传管理(OkServer)";
model9.des = "1.这个同上,也属于OkServer依赖中的功能\n" +
"2.同样该包的功能OkGo完全可以胜任\n" +
"3.上传只是简单上传管理,不支持断点上传或者分片上传";
model9.type = 0;
model9.title = "";
model9.des = "下面是OkServer包的使用方法";
model9.type = 1;
items.add(model9);

OkHttpModel model10 = new OkHttpModel();
model10.title = "下载管理(OkServer)";
model10.des = "1.这个属于OkServer依赖中的功能,并不属于OkGo\n" +
"2.这个包维护较少,一般情况下,不做特殊的下载管理功能,OkGo完全可以胜任\n" +
"3.相比OkGo主要是多了断点下载和下载状态的管理";
model10.type = 0;
items.add(model10);

OkHttpModel model11 = new OkHttpModel();
model11.title = "上传管理(OkServer)";
model11.des = "1.这个同上,也属于OkServer依赖中的功能\n" +
"2.同样该包的功能OkGo完全可以胜任\n" +
"3.上传只是简单上传管理,不支持断点上传或者分片上传";
model11.type = 0;
items.add(model11);
}

@Override
Expand Down Expand Up @@ -185,13 +203,14 @@ public void bind(int position, OkHttpModel model) {

@Override
public void onClick(View v) {
if (position == 1) startActivity(new Intent(MainActivity.this, OkHttpActivity.class));
if (position == 2) startActivity(new Intent(MainActivity.this, JsonRequestActivity.class));
if (position == 3) startActivity(new Intent(MainActivity.this, FileDownloadActivity.class));
if (position == 4) startActivity(new Intent(MainActivity.this, FormUploadActivity.class));
if (position == 5) startActivity(new Intent(MainActivity.this, CacheDemoActivity.class));
if (position == 7) startActivity(new Intent(MainActivity.this, DownloadActivity.class));
if (position == 8) startActivity(new Intent(MainActivity.this, UploadActivity.class));
if (position == 1) startActivity(new Intent(MainActivity.this, RxActivity.class));
if (position == 3) startActivity(new Intent(MainActivity.this, OkHttpActivity.class));
if (position == 4) startActivity(new Intent(MainActivity.this, JsonRequestActivity.class));
if (position == 5) startActivity(new Intent(MainActivity.this, FileDownloadActivity.class));
if (position == 6) startActivity(new Intent(MainActivity.this, FormUploadActivity.class));
if (position == 7) startActivity(new Intent(MainActivity.this, CacheDemoActivity.class));
if (position == 9) startActivity(new Intent(MainActivity.this, DownloadActivity.class));
if (position == 10) startActivity(new Intent(MainActivity.this, UploadActivity.class));
}
}

Expand Down
26 changes: 25 additions & 1 deletion app/src/main/java/com/lzy/demo/base/BaseActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lzy.demo.base;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -12,9 +13,10 @@
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.lzy.imagepicker.view.SystemBarTintManager;
import com.lzy.demo.R;
import com.lzy.imagepicker.view.SystemBarTintManager;

import butterknife.ButterKnife;

Expand Down Expand Up @@ -123,4 +125,26 @@ public void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, String title)
public void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, int resTitle) {
initToolBar(toolbar, homeAsUpEnabled, getString(resTitle));
}

public void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

private ProgressDialog dialog;

public void showLoading() {
if (dialog != null && dialog.isShowing()) return;
dialog = new ProgressDialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(false);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("请求网络中...");
dialog.show();
}

public void dismissLoading() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
}
41 changes: 26 additions & 15 deletions app/src/main/java/com/lzy/demo/base/BaseDetailActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,21 @@ public void setContentView(View view, ViewGroup.LayoutParams params) {
}

protected <T> void handleResponse(T data, Call call, Response response) {
requestState.setText("请求成功 请求方式:" + call.request().method() + "\n" + "url:" + call.request().url());
StringBuilder sb;
if (call != null) {
requestState.setText("请求成功 请求方式:" + call.request().method() + "\n" + "url:" + call.request().url());

Headers requestHeadersString = call.request().headers();
Set<String> requestNames = requestHeadersString.names();
StringBuilder sb = new StringBuilder();
for (String name : requestNames) {
sb.append(name).append(" : ").append(requestHeadersString.get(name)).append("\n");
Headers requestHeadersString = call.request().headers();
Set<String> requestNames = requestHeadersString.names();
sb = new StringBuilder();
for (String name : requestNames) {
sb.append(name).append(" : ").append(requestHeadersString.get(name)).append("\n");
}
requestHeaders.setText(sb.toString());
} else {
requestState.setText("--");
requestHeaders.setText("--");
}
requestHeaders.setText(sb.toString());

if (data == null) {
responseData.setText("--");
} else {
Expand Down Expand Up @@ -151,15 +156,21 @@ protected <T> void handleResponse(T data, Call call, Response response) {
}

protected void handleError(Call call, Response response) {
requestState.setText("请求失败 请求方式:" + call.request().method() + "\n" + "url:" + call.request().url());
StringBuilder sb;
if (call != null) {
requestState.setText("请求失败 请求方式:" + call.request().method() + "\n" + "url:" + call.request().url());

Headers requestHeadersString = call.request().headers();
Set<String> requestNames = requestHeadersString.names();
StringBuilder sb = new StringBuilder();
for (String name : requestNames) {
sb.append(name).append(" : ").append(requestHeadersString.get(name)).append("\n");
Headers requestHeadersString = call.request().headers();
Set<String> requestNames = requestHeadersString.names();
sb = new StringBuilder();
for (String name : requestNames) {
sb.append(name).append(" : ").append(requestHeadersString.get(name)).append("\n");
}
requestHeaders.setText(sb.toString());
} else {
requestState.setText("--");
requestHeaders.setText("--");
}
requestHeaders.setText(sb.toString());

responseData.setText("--");
if (response != null) {
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/lzy/demo/callback/JsonCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public void onBefore(BaseRequest request) {
*/
@Override
public T convertSuccess(Response response) throws Exception {

//以下代码是通过泛型解析实际参数,泛型必须传
JsonConvert<T> convert = JsonConvert.create();
Type genType = getClass().getGenericSuperclass();
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
Type type = params[0];
if (!(type instanceof ParameterizedType)) throw new IllegalStateException("没有填写泛型参数");
convert.setType((ParameterizedType) type);

JsonConvert<T> convert = new JsonConvert<>();
convert.setType(params[0]);
T t = convert.convertSuccess(response);
response.close();
return t;
Expand Down
Loading

0 comments on commit b4ebbea

Please sign in to comment.