RetrofitManager
public class RetrofitManager {
public static OkHttpClient client = new OkHttpClient.Builder()
.build();
public static ApiService apiService = new Retrofit.Builder()
.baseUrl("http://120.27.23.105/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.client(client)
.build()
.create(ApiService.class);
public static void get(String url, Map<String,String> map,Observer observer){
apiService.get(url,map)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
public static void post(String url,Map<String,String> map,Observer observer){
apiService.post(url,map)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
}
BaseObserver
public abstract class BaseObserver<T> implements Observer<String> {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
try {
Type genType = getClass().getGenericSuperclass();
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
Class entityClass = (Class) params[0];
Gson gson = new Gson();
T t = (T)gson.fromJson(s,entityClass);
success(t);
} catch (Exception e) {
failure(1001);
e.printStackTrace();
}
}
@Override
public void onError(Throwable e) {
try {
if(e != null){
if(e instanceof HttpException){
failure(HTTP_ERROR);
} else if(e instanceof SocketException){
failure(NET_WORK_ERROR);
}else {
failure(UNKNOW_ERROR);
}
}else {
failure(UNKNOW_ERROR);
}
e.printStackTrace() ;
} catch (Exception e1) {
failure(UNKNOW_ERROR);
e1.printStackTrace();
}
}
@Override
public void onComplete() {
}
//
/**
* code
* 1000 UNKNOW_ERROR 未知错误
* 1001 json 转化异常 parse error
* 1002 当前网络不可用 java.net.SocketException: Network is unreachable 超时
* 1003 服务器不可用 401 402 403 500 502 503 504
* @param code
*/
public static final int UNKNOW_ERROR = 1000;
public static final int JSON_FORMAT_ERROR = 1001;
public static final int NET_WORK_ERROR = 1002;
public static final int HTTP_ERROR = 1003;
public abstract void success(T t);
public abstract void failure(int code);
}
ApiService
public interface ApiService {
@GET
Observable<String> get(@Url String url , @QueryMap Map<String,String> map);
@FormUrlEncoded
@POST
Observable<String> post(@Url String url, @FieldMap Map<String,String> map);
}
MyPopupWindow
public class MyPopupWindow {
private static PopupWindow mPopWindow;
public static void showPopupWindow(final ViewPager mViewPager, Context context, ImageView mImageView) {
View contentView = LayoutInflater.from(context).inflate(R.layout.popupwindow, null);
mPopWindow = new PopupWindow(contentView);
mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);
TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);
TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);
tv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(1,true);
mPopWindow.dismiss();
}
});
tv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(2,true);
mPopWindow.dismiss();
}
});
tv3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(3,true);
mPopWindow.dismiss();
}
});
mPopWindow.showAsDropDown(mImageView);
}
}
popupwindow
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
android:paddingBottom="2dp">
<TextView
android:textSize="20sp"
android:id="@+id/pop_computer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="待支付"/>
<TextView
android:textSize="20sp"
android:id="@+id/pop_financial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已支付"/>
/>
<TextView
android:textSize="20sp"
android:id="@+id/pop_manage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已取消"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<Button
android:layout_width="35dp"
android:layout_height="35dp"
android:text="-"
android:id="@+id/jian"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit"
/>
<Button
android:text="+"
android:id="@+id/jia"
android:layout_width="35dp"
android:layout_height="35dp" />
</LinearLayout>