GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 添加Retrofit和xUtils3网络示例demo · androidok/AndroidStudyDemo@6076af6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • NetworkFrameStudy
    • build.gradle
    • src/main
      • AndroidManifest.xml
      • java/com/cheng/networkframestudy
        • C.java
        • retrofit
          • RetrofitDemoActivity.java
          • api
            • IDCardService.java
            • MobilePhoneService.java
          • convert
            • GsonRequestBodyConverter.java
            • GsonResponseBodyConverter.java
            • MyGsonConverter.java
          • model
            • IDCardInfo.java
        • xutils
          • XUtils3DemoActivity.java
          • frame
            • XUtil3HttpUtil.java
            • callback
              • HttpCallBack.java
      • res/layout
        • activity_retrofitdemo.xml
        • activity_xutils3demo.xml
  • Utils/src/main/java/com/cheng/utils/file
    • FileUtil.java
    • SDUtil.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎NetworkFrameStudy/build.gradle‎

Copy file name to clipboardExpand all lines: NetworkFrameStudy/build.gradle
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ dependencies {
2727
compile 'com.jakewharton:butterknife:7.0.1'
2828
// OkHttp3
2929
compile 'com.squareup.okhttp3:okhttp:3.1.2'
30+
// Okio
31+
compile 'com.squareup.okio:okio:1.6.0'
3032
// Gson
3133
compile 'com.google.code.gson:gson:2.4'
3234
// Android-Async-Http
3335
compile 'com.loopj.android:android-async-http:1.4.9'
3436
// xUtils3
35-
compile 'org.xutils:xutils:3.3.16'
37+
compile 'org.xutils:xutils:3.3.22'
3638
// Retrofit
3739
compile 'com.squareup.retrofit2:retrofit:2.0.0'
3840
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
Collapse file

‎NetworkFrameStudy/src/main/AndroidManifest.xml‎

Copy file name to clipboardExpand all lines: NetworkFrameStudy/src/main/AndroidManifest.xml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<application
99
android:allowBackup="true"
10+
android:name=".BaseApp"
1011
android:icon="@mipmap/ic_launcher"
1112
android:label="@string/app_name"
1213
android:theme="@style/AppTheme">
Collapse file

‎NetworkFrameStudy/src/main/java/com/cheng/networkframestudy/C.java‎

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 numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
public class C {
77

8+
public static final String APP_NAME = "AndroidStudyDemo";
9+
810
public static final class baseurl {
911
public static final String TAOBAO_IP_SEARCH_BASE_URL = "http://ip.taobao.com";
1012
public static final String BAIDU_API_STORE = "http://apis.baidu.com/apistore/";
@@ -16,6 +18,7 @@ public static final class apikey {
1618

1719
public static final class apiurl {
1820
public static final String MOBILE_PHONE_SERVICE = "/mobilephoneservice/mobilephone";
21+
public static final String ID_CARD_SERVICE = "/idservice/id";
1922
}
2023

2124
}
Collapse file

‎NetworkFrameStudy/src/main/java/com/cheng/networkframestudy/retrofit/RetrofitDemoActivity.java‎

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 numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88

99
import com.cheng.networkframestudy.C;
1010
import com.cheng.networkframestudy.R;
11+
import com.cheng.networkframestudy.retrofit.api.IDCardService;
1112
import com.cheng.networkframestudy.retrofit.api.IPService;
1213
import com.cheng.networkframestudy.retrofit.api.MobilePhoneService;
14+
import com.cheng.networkframestudy.retrofit.convert.MyGsonConverter;
15+
import com.cheng.networkframestudy.retrofit.model.IDCardInfo;
1316
import com.cheng.networkframestudy.retrofit.model.IPDetail;
1417
import com.cheng.networkframestudy.retrofit.model.TelphoneOwnershipOfLand;
1518

1619
import retrofit2.Call;
1720
import retrofit2.Callback;
21+
import retrofit2.Converter;
1822
import retrofit2.Response;
1923
import retrofit2.Retrofit;
2024
import retrofit2.converter.gson.GsonConverterFactory;
@@ -45,6 +49,9 @@ public void onClick(View view) {
4549
case R.id.btn_telsearch:
4650
telSearch();
4751
break;
52+
case R.id.btn_idsearch:
53+
idCardSearch();
54+
break;
4855
}
4956
}
5057

@@ -96,6 +103,30 @@ public void onFailure(Call<TelphoneOwnershipOfLand> call, Throwable t) {
96103
});
97104
}
98105

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+
99130
private void showLoading() {
100131
mLoadingPB.setVisibility(View.VISIBLE);
101132
}
@@ -104,4 +135,5 @@ private void hideLoading() {
104135
mLoadingPB.setVisibility(View.GONE);
105136
}
106137

138+
107139
}
Collapse file

‎NetworkFrameStudy/src/main/java/com/cheng/networkframestudy/retrofit/api/IDCardService.java‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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

‎NetworkFrameStudy/src/main/java/com/cheng/networkframestudy/retrofit/api/MobilePhoneService.java‎

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 numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import retrofit2.Call;
77
import retrofit2.http.GET;
8+
import retrofit2.http.Header;
89
import retrofit2.http.Headers;
910
import retrofit2.http.Query;
1011

Collapse file

‎NetworkFrameStudy/src/main/java/com/cheng/networkframestudy/retrofit/convert/GsonRequestBodyConverter.java‎

Copy file name to clipboard
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
* author youxuan E-mail:[email protected]
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

‎NetworkFrameStudy/src/main/java/com/cheng/networkframestudy/retrofit/convert/GsonResponseBodyConverter.java‎

Copy file name to clipboard
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
* author youxuan E-mail:[email protected]
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

‎NetworkFrameStudy/src/main/java/com/cheng/networkframestudy/retrofit/convert/MyGsonConverter.java‎

Copy file name to clipboard
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
* author youxuan E-mail:[email protected]
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

‎NetworkFrameStudy/src/main/java/com/cheng/networkframestudy/retrofit/model/IDCardInfo.java‎

Copy file name to clipboard
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.