Android_RxJava + Retrofit + MVP雏形

写在最前:
简单的retrofit在线请求天气情况
配合rxjava简单处理获取的数据
mvp雏形,不过太懒了不想写了
awq


1.添加依赖

  compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.0.9'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
    compile files('libs/butterknife-7.0.0.jar')
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

2.配置权限

<uses-permission android:name="android.permission.INTERNET" />

3.主要逻辑

package com.oblivion.rxjava.model;


import com.oblivion.rxjava.bean.HomeBeam;
import com.oblivion.rxjava.envity.MyEnvity;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * github : https://github.com/oblivion0001/AndroidStudioProjects
 * Blog : http://blog.csdn.net/qq_16666847
 * Created by oblivion on 2016/11/25.
 */
public class RetrofitModel {

    public RetrofitModel() {

    }

    public HomeBeam getResString(String etCity) {
        Retrofit retrofit = new Retrofit.Builder()
                //http://api.map.baidu.com/telematics/v3/weather?location=%E5%98%89%E5%85%B4&output=json&ak=qXYfeO4jG61DfcHeAiCbdIPKENySbvx1
                .baseUrl("http://api.map.baidu.com/telematics/v3/")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MyEnvity myEnvity = retrofit.create(MyEnvity.class);
        myEnvity.getCall("etCity")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())//在主线程更新
                .subscribe(new Subscriber<HomeBeam>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        getHomeBean(e.toString(),null);
                    }

                    @Override
                    public void onNext(HomeBeam homeBeam) {
                       getHomeBean(null,homeBeam);
                    }
                });

        return null;
    }

    /**
     *  @param homeBeam
     * @param
     */
    public  HomeBeam getHomeBean(String error, HomeBeam homeBeam) {
        return homeBeam;
    }
}

4.MainTivity

package com.oblivion.rxjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.oblivion.rxjava.model.RetrofitModel;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends AppCompatActivity {

    @Bind(R.id.tv_data)
    TextView tvData;
    @Bind(R.id.tv_clothes)
    TextView tvClothes;
    @Bind(R.id.tv_car)
    TextView tvCar;
    @Bind(R.id.tv_goto)
    TextView tvGoto;
    @Bind(R.id.tv_seek)
    TextView tvSeek;
    @Bind(R.id.tv_play)
    TextView tvPlay;
    @Bind(R.id.tv_sun)
    TextView tvSun;
    @Bind(R.id.bt_check)
    Button btCheck;
    @Bind(R.id.et_check)
    EditText etCheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.bt_check)
    public void onClick() {
        String etCity = etCheck.getText().toString().trim();
        RetrofitModel retrofitModel = new RetrofitModel();
        retrofitModel.getResString(etCity);
    }
}

5.布局

<?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">

    <EditText
        android:id="@+id/et_check"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="请输入要查询的城市" />

    <Button
        android:id="@+id/bt_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询" />

    <FrameLayout
        android:id="@+id/fl_success"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_data"
                style="@style/textStyle"
                android:text="日期" />

            <TextView
                android:id="@+id/tv_clothes"
                style="@style/textStyle" />

            <TextView
                android:id="@+id/tv_car"
                style="@style/textStyle" />

            <TextView
                android:id="@+id/tv_goto"
                style="@style/textStyle" />

            <TextView
                android:id="@+id/tv_seek"
                style="@style/textStyle" />

            <TextView
                android:id="@+id/tv_play"
                style="@style/textStyle" />

            <TextView
                android:id="@+id/tv_sun"
                style="@style/textStyle" />
        </LinearLayout>

    </FrameLayout>


</LinearLayout>

5.MyEntiry

package com.oblivion.rxjava.envity;


import com.oblivion.rxjava.bean.HomeBeam;

import retrofit2.http.GET;
import retrofit2.http.Query;
import rx.Observable;

/**
 * github : https://github.com/oblivion0001/AndroidStudioProjects
 * Blog : http://blog.csdn.net/qq_16666847
 * Created by oblivion on 2016/11/22.
 */
public interface MyEnvity {
    @GET("weather?ak=qXYfeO4jG61DfcHeAiCbdIPKENySbvx1&output=json")
    Observable<HomeBeam> getCall(@Query("location") String path);
}

github源码

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值