GeezerVison is showing github.com — 添加Retrofit和xUtils3网络示例demo · androidok/AndroidStudyDemo@6076af6
File treeExpand file treeCollapse file tree Open diff view settings java/com/cheng/networkframestudy
Utils/src/main/java/com/cheng/utils/file
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: NetworkFrameStudy/build.gradle +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,12 +27,14 @@ dependencies { | 27 | 27 | compile 'com.jakewharton:butterknife:7.0.1' | 28 | 28 | // OkHttp3 | 29 | 29 | compile 'com.squareup.okhttp3:okhttp:3.1.2' | | 30 | + // Okio | | 31 | + compile 'com.squareup.okio:okio:1.6.0' | 30 | 32 | // Gson | 31 | 33 | compile 'com.google.code.gson:gson:2.4' | 32 | 34 | // Android-Async-Http | 33 | 35 | compile 'com.loopj.android:android-async-http:1.4.9' | 34 | 36 | // xUtils3 | 35 | | - compile 'org.xutils:xutils:3.3.16' | | 37 | + compile 'org.xutils:xutils:3.3.22' | 36 | 38 | // Retrofit | 37 | 39 | compile 'com.squareup.retrofit2:retrofit:2.0.0' | 38 | 40 | compile 'com.squareup.retrofit2:converter-gson:2.0.0' | |
Collapse file Copy file name to clipboardExpand all lines: NetworkFrameStudy/src/main/AndroidManifest.xml +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 |
| 8 | 8 | <application | 9 | 9 | android:allowBackup="true" | | 10 | + android:name=".BaseApp" | 10 | 11 | android:icon="@mipmap/ic_launcher" | 11 | 12 | android:label="@string/app_name" | 12 | 13 | android:theme="@style/AppTheme"> | |
Collapse file Copy file name to clipboardExpand all lines: NetworkFrameStudy/src/main/java/com/cheng/networkframestudy/C.java +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | */ | 6 | 6 | public class C { | 7 | 7 |
| | 8 | + public static final String APP_NAME = "AndroidStudyDemo"; | | 9 | + | 8 | 10 | public static final class baseurl { | 9 | 11 | public static final String TAOBAO_IP_SEARCH_BASE_URL = "http://ip.taobao.com"; | 10 | 12 | public static final String BAIDU_API_STORE = "http://apis.baidu.com/apistore/"; | @@ -16,6 +18,7 @@ public static final class apikey { | 16 | 18 |
| 17 | 19 | public static final class apiurl { | 18 | 20 | public static final String MOBILE_PHONE_SERVICE = "/mobilephoneservice/mobilephone"; | | 21 | + public static final String ID_CARD_SERVICE = "/idservice/id"; | 19 | 22 | } | 20 | 23 |
| 21 | 24 | } |
Collapse file Copy file name to clipboardExpand all lines: NetworkFrameStudy/src/main/java/com/cheng/networkframestudy/retrofit/RetrofitDemoActivity.java +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 |
| 9 | 9 | import com.cheng.networkframestudy.C; | 10 | 10 | import com.cheng.networkframestudy.R; | | 11 | +import com.cheng.networkframestudy.retrofit.api.IDCardService; | 11 | 12 | import com.cheng.networkframestudy.retrofit.api.IPService; | 12 | 13 | import com.cheng.networkframestudy.retrofit.api.MobilePhoneService; | | 14 | +import com.cheng.networkframestudy.retrofit.convert.MyGsonConverter; | | 15 | +import com.cheng.networkframestudy.retrofit.model.IDCardInfo; | 13 | 16 | import com.cheng.networkframestudy.retrofit.model.IPDetail; | 14 | 17 | import com.cheng.networkframestudy.retrofit.model.TelphoneOwnershipOfLand; | 15 | 18 |
| 16 | 19 | import retrofit2.Call; | 17 | 20 | import retrofit2.Callback; | | 21 | +import retrofit2.Converter; | 18 | 22 | import retrofit2.Response; | 19 | 23 | import retrofit2.Retrofit; | 20 | 24 | import retrofit2.converter.gson.GsonConverterFactory; | @@ -45,6 +49,9 @@ public void onClick(View view) { | 45 | 49 | case R.id.btn_telsearch: | 46 | 50 | telSearch(); | 47 | 51 | break; | | 52 | + case R.id.btn_idsearch: | | 53 | + idCardSearch(); | | 54 | + break; | 48 | 55 | } | 49 | 56 | } | 50 | 57 |
| @@ -96,6 +103,30 @@ public void onFailure(Call<TelphoneOwnershipOfLand> call, Throwable t) { | 96 | 103 | }); | 97 | 104 | } | 98 | 105 |
| | 106 | + private void idCardSearch() { | | 107 | + Retrofit retrofit = new Retrofit.Builder() | | 108 | + .baseUrl(C.baseurl.BAIDU_API_STORE) | | 109 | + .addConverterFactory(MyGsonConverter.create()) | | 110 | + .build(); | | 111 | + IDCardService idCardService = retrofit.create(IDCardService.class); | | 112 | + showLoading(); | | 113 | + Call<IDCardInfo> idCardInfoCall = idCardService.getIDCardInfo("420984198704207896"); | | 114 | + idCardInfoCall.enqueue(new Callback<IDCardInfo>() { | | 115 | + @Override | | 116 | + public void onResponse(Call<IDCardInfo> call, Response<IDCardInfo> response) { | | 117 | + hideLoading(); | | 118 | + IDCardInfo idCardInfo = response.body(); | | 119 | + mShowResultTV.setText(idCardInfo.toString()); | | 120 | + } | | 121 | + | | 122 | + @Override | | 123 | + public void onFailure(Call<IDCardInfo> call, Throwable t) { | | 124 | + hideLoading(); | | 125 | + mShowResultTV.setText("Error"); | | 126 | + } | | 127 | + }); | | 128 | + } | | 129 | + | 99 | 130 | private void showLoading() { | 100 | 131 | mLoadingPB.setVisibility(View.VISIBLE); | 101 | 132 | } | @@ -104,4 +135,5 @@ private void hideLoading() { | 104 | 135 | mLoadingPB.setVisibility(View.GONE); | 105 | 136 | } | 106 | 137 |
| | 138 | + | 107 | 139 | } |
Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.cheng.networkframestudy.retrofit.api; | | 2 | + | | 3 | +import com.cheng.networkframestudy.C; | | 4 | +import com.cheng.networkframestudy.retrofit.model.IDCardInfo; | | 5 | +import com.cheng.networkframestudy.retrofit.model.TelphoneOwnershipOfLand; | | 6 | + | | 7 | +import retrofit2.Call; | | 8 | +import retrofit2.http.GET; | | 9 | +import retrofit2.http.Headers; | | 10 | +import retrofit2.http.Query; | | 11 | + | | 12 | +/** | | 13 | + * 身份证查询服务 | | 14 | + */ | | 15 | +public interface IDCardService { | | 16 | + | | 17 | + @Headers("apikey:" + C.apikey.BAIDU_API_STORE_KEY) | | 18 | + @GET(C.apiurl.ID_CARD_SERVICE) | | 19 | + Call<IDCardInfo> getIDCardInfo(@Query("id") String id); | | 20 | +} |
Collapse file Copy file name to clipboardExpand all lines: NetworkFrameStudy/src/main/java/com/cheng/networkframestudy/retrofit/api/MobilePhoneService.java +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 |
| 6 | 6 | import retrofit2.Call; | 7 | 7 | import retrofit2.http.GET; | | 8 | +import retrofit2.http.Header; | 8 | 9 | import retrofit2.http.Headers; | 9 | 10 | import retrofit2.http.Query; | 10 | 11 |
| |
Collapse file Copy file name to clipboard +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.cheng.networkframestudy.retrofit.convert; | | 2 | + | | 3 | +import com.google.gson.Gson; | | 4 | + | | 5 | +import java.io.IOException; | | 6 | +import java.io.OutputStreamWriter; | | 7 | +import java.io.Writer; | | 8 | +import java.lang.reflect.Type; | | 9 | +import java.nio.charset.Charset; | | 10 | + | | 11 | +import okhttp3.MediaType; | | 12 | +import okhttp3.RequestBody; | | 13 | +import okio.Buffer; | | 14 | +import retrofit2.Converter; | | 15 | + | | 16 | +/** | | 17 | + * ClassName GsonRequestBodyConverter | | 18 | + * Description | | 19 | + * Company | | 20 | + | | 21 | + * date createTime:2015/12/24 12:39 | | 22 | + * version | | 23 | + */ | | 24 | +public class GsonRequestBodyConverter<T> implements Converter<T, RequestBody> { | | 25 | + private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8"); | | 26 | + private static final Charset UTF_8 = Charset.forName("UTF-8"); | | 27 | + | | 28 | + private final Gson gson; | | 29 | + private final Type type; | | 30 | + | | 31 | + GsonRequestBodyConverter(Gson gson, Type type) { | | 32 | + this.gson = gson; | | 33 | + this.type = type; | | 34 | + } | | 35 | + | | 36 | + @Override public RequestBody convert(T value) throws IOException { | | 37 | + Buffer buffer = new Buffer(); | | 38 | + Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); | | 39 | + try { | | 40 | + gson.toJson(value, type, writer); | | 41 | + writer.flush(); | | 42 | + } catch (IOException e) { | | 43 | + throw new AssertionError(e); // Writing to Buffer does no I/O. | | 44 | + } | | 45 | + return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); | | 46 | + } | | 47 | +} |
Collapse file Copy file name to clipboard +52Lines changed: 52 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.cheng.networkframestudy.retrofit.convert; | | 2 | + | | 3 | +import com.google.gson.Gson; | | 4 | +import com.google.gson.stream.JsonReader; | | 5 | + | | 6 | +import java.io.Closeable; | | 7 | +import java.io.IOException; | | 8 | +import java.io.Reader; | | 9 | +import java.lang.reflect.Type; | | 10 | + | | 11 | +import okhttp3.ResponseBody; | | 12 | +import retrofit2.Converter; | | 13 | + | | 14 | +/** | | 15 | + * ClassName GsonResponseBodyConverter | | 16 | + * Description | | 17 | + * Company | | 18 | + | | 19 | + * date createTime:2015/12/24 12:39 | | 20 | + * version | | 21 | + */ | | 22 | +public class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> { | | 23 | + private final Gson gson; | | 24 | + private final Type type; | | 25 | + | | 26 | + GsonResponseBodyConverter(Gson gson, Type type) { | | 27 | + this.gson = gson; | | 28 | + this.type = type; | | 29 | + } | | 30 | + | | 31 | + @Override | | 32 | + public T convert(ResponseBody value) throws IOException { | | 33 | + Reader reader = value.charStream(); | | 34 | + try { | | 35 | + | | 36 | + JsonReader jsonReader = new JsonReader(reader); | | 37 | + jsonReader.setLenient(true); | | 38 | + return gson.fromJson(jsonReader, type); | | 39 | + // return gson.fromJson(reader, type); | | 40 | + } finally { | | 41 | + closeQuietly(reader); | | 42 | + } | | 43 | + } | | 44 | + | | 45 | + static void closeQuietly(Closeable closeable) { | | 46 | + if (closeable == null) return; | | 47 | + try { | | 48 | + closeable.close(); | | 49 | + } catch (IOException ignored) { | | 50 | + } | | 51 | + } | | 52 | +} |
Collapse file Copy file name to clipboard +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.cheng.networkframestudy.retrofit.convert; | | 2 | + | | 3 | +import com.google.gson.Gson; | | 4 | +import com.google.gson.TypeAdapter; | | 5 | +import com.google.gson.reflect.TypeToken; | | 6 | + | | 7 | +import java.lang.annotation.Annotation; | | 8 | +import java.lang.reflect.Type; | | 9 | + | | 10 | +import okhttp3.RequestBody; | | 11 | +import okhttp3.ResponseBody; | | 12 | +import retrofit2.Converter; | | 13 | +import retrofit2.Retrofit; | | 14 | + | | 15 | +/** | | 16 | + * ClassName MyGsonConverter | | 17 | + * Description | | 18 | + * Company | | 19 | + | | 20 | + * date createTime:2015/12/24 12:39 | | 21 | + * version | | 22 | + */ | | 23 | +public class MyGsonConverter extends Converter.Factory { | | 24 | + public static MyGsonConverter create() { | | 25 | + return create(new Gson()); | | 26 | + } | | 27 | + | | 28 | + public static MyGsonConverter create(Gson gson) { | | 29 | + return new MyGsonConverter(gson); | | 30 | + } | | 31 | + | | 32 | + private final Gson gson; | | 33 | + | | 34 | + private MyGsonConverter(Gson gson) { | | 35 | + if (gson == null) throw new NullPointerException("gson == null"); | | 36 | + this.gson = gson; | | 37 | + } | | 38 | + | | 39 | + @Override | | 40 | + public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, | | 41 | + Retrofit retrofit) { | | 42 | + return new GsonResponseBodyConverter<>(gson, type); | | 43 | + } | | 44 | + | | 45 | + @Override | | 46 | + public Converter<?, RequestBody> requestBodyConverter(Type type, | | 47 | + Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { | | 48 | + return new GsonRequestBodyConverter<>(gson, type); | | 49 | + } | | 50 | +} |
Collapse file Copy file name to clipboard +77Lines changed: 77 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.cheng.networkframestudy.retrofit.model; | | 2 | + | | 3 | +/** | | 4 | + * Created by Android on 2016/3/20. | | 5 | + */ | | 6 | +public class IDCardInfo { | | 7 | + | | 8 | + /** | | 9 | + * errNum : 0 | | 10 | + * retMsg : success | | 11 | + * retData : {"sex":"M","birthday":"1987-04-20","address":"湖北省孝感市汉川市"} | | 12 | + */ | | 13 | + | | 14 | + private int errNum; | | 15 | + private String retMsg; | | 16 | + /** | | 17 | + * sex : M | | 18 | + * birthday : 1987-04-20 | | 19 | + * address : 湖北省孝感市汉川市 | | 20 | + */ | | 21 | + | | 22 | + private RetDataEntity retData; | | 23 | + | | 24 | + public void setErrNum(int errNum) { | | 25 | + this.errNum = errNum; | | 26 | + } | | 27 | + | | 28 | + public void setRetMsg(String retMsg) { | | 29 | + this.retMsg = retMsg; | | 30 | + } | | 31 | + | | 32 | + public void setRetData(RetDataEntity retData) { | | 33 | + this.retData = retData; | | 34 | + } | | 35 | + | | 36 | + public int getErrNum() { | | 37 | + return errNum; | | 38 | + } | | 39 | + | | 40 | + public String getRetMsg() { | | 41 | + return retMsg; | | 42 | + } | | 43 | + | | 44 | + public RetDataEntity getRetData() { | | 45 | + return retData; | | 46 | + } | | 47 | + | | 48 | + public static class RetDataEntity { | | 49 | + private String sex; | | 50 | + private String birthday; | | 51 | + private String address; | | 52 | + | | 53 | + public void setSex(String sex) { | | 54 | + this.sex = sex; | | 55 | + } | | 56 | + | | 57 | + public void setBirthday(String birthday) { | | 58 | + this.birthday = birthday; | | 59 | + } | | 60 | + | | 61 | + public void setAddress(String address) { | | 62 | + this.address = address; | | 63 | + } | | 64 | + | | 65 | + public String getSex() { | | 66 | + return sex; | | 67 | + } | | 68 | + | | 69 | + public String getBirthday() { | | 70 | + return birthday; | | 71 | + } | | 72 | + | | 73 | + public String getAddress() { | | 74 | + return address; | | 75 | + } | | 76 | + } | | 77 | +} |
|