标签:
Retrofit:Android和Java的类型安全的HTTP客户端。
<span style="font-size:14px;">public interface GitHubService { @GET("users/{user}/repos") Call<List<Repo>> listRepos(@Path("user") String user); }</span>
<span style="font-size:14px;">Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .build(); GitHubService service = retrofit.create(GitHubService.class);</span>
<span style="font-size:14px;">Call<List<Repo>> repos = service.listRepos("octocat");</span>
<span style="font-size:14px;">@GET("users/list")</span>
<span style="font-size:14px;">@GET("users/list?sort=desc")</span>
<span style="font-size:14px;">@GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId);</span>
<span style="font-size:14px;">@GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort);</span>
<span style="font-size:14px;">@GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId, @QueryMap Map<String, String> options);</span>
<span style="font-size:14px;">@POST("users/new") Call<User> createUser(@Body User user);</span>
<span style="font-size:14px;">@FormUrlEncoded @POST("user/edit") Call<User> updateUser(@Field("first_name") String first, @Field("last_name") String last);</span>
<span style="font-size:14px;">@Multipart @PUT("user/photo") Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description);</span>
<span style="font-size:14px;">@Headers("Cache-Control: max-age=640000") @GET("widget/list") Call<List<Widget>> widgetList();</span>
<span style="font-size:14px;">@Headers({ "Accept: application/vnd.github.v3.full+json", "User-Agent: Retrofit-Sample-App" }) @GET("users/{username}") Call<User> getUser(@Path("username") String username);</span>
<span style="font-size:14px;">@GET("user") Call<User> getUser(@Header("Authorization") String authorization)</span>
<span style="font-size:14px;">Gson: com.squareup.retrofit2:converter-gson</span>
<span style="font-size:14px;">Jackson: com.squareup.retrofit2:converter-jackson</span>
<span style="font-size:14px;">Moshi: com.squareup.retrofit2:converter-moshi Protobuf: com.squareup.retrofit2:converter-protobuf Wire: com.squareup.retrofit2:converter-wire Simple XML: com.squareup.retrofit2:converter-simplexml Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars</span>
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
<span style="font-size:14px;">public interface UserCardnoService { //基本信息查询 @GET("/idcard/index") Call<InforBean> getInfoResult(@Query("key") String key, @Query("cardno") String cardno); //信息是否泄漏 @GET("/idcard/leak") Call<LeakBean> getLeakResult(@Query("key") String key, @Query("cardno") String cardno); //身份证是否挂失 @GET("/idcard/loss") Call<LossBean> getLossResult(@Query("key") String key, @Query("cardno") String cardno); }</span>
<span style="font-size:14px;">public class InforBean { /** * resultcode : 200 * reason : 成功的返回 * result : {"area":"浙江省温州市平阳县","sex":"男","birthday":"1989年03月08日"} */ public String resultcode; public String reason; public int error_code; /** * area : 浙江省温州市平阳县 * sex : 男 * birthday : 1989年03月08日 */ public ResultBean result; public static class ResultBean { public String area; public String sex; public String birthday; public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } } public String getResultcode() { return resultcode; } public void setResultcode(String resultcode) { this.resultcode = resultcode; } public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } public int getError_code() { return error_code; } public void setError_code(int error_code) { this.error_code = error_code; } public ResultBean getResult() { return result; } public void setResult(ResultBean result) { this.result = result; } }</span>
<span style="font-size:14px;">public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener { public static final String TAG = "UserInfoActivity"; private EditText mEtInput; private Button btnQuery; private TextView mTvArea; private TextView mTvSex; private TextView mTvBirthday; private LinearLayout llInfo; private String mCardno; private Context mContext; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = UserInfoActivity.this; setContentView(R.layout.base_layout); initView(); } private void initView() { mEtInput = (EditText) findViewById(R.id.etInput); btnQuery = (Button) findViewById(R.id.btnQuery); mTvArea = (TextView) findViewById(R.id.tvArea); mTvSex = (TextView) findViewById(R.id.tvSex); mTvBirthday = (TextView) findViewById(R.id.tvBirthday); llInfo = (LinearLayout) findViewById(R.id.ll_Info); btnQuery.setOnClickListener(this); } @Override public void onClick(View v) { mCardno = mEtInput.getText().toString().trim(); if (!mCardno.matches(RegexpUtils.USER_CARD)) { Toast.makeText(mContext, "输入有误,请重新输入!", Toast.LENGTH_SHORT).show(); } else { getNetData(); } } private void getNetData() { //构建rtrofit实例 Retrofit retrofit = new Retrofit .Builder() .baseUrl(GlobalContact.BASEURL) .addConverterFactory(GsonConverterFactory.create()) .build(); UserCardnoService infoService = retrofit.create(UserCardnoService.class); Call<InforBean> call = infoService.getInfoResult(GlobalContact.KEY, mCardno); call.enqueue(new Callback<InforBean>() { @Override public void onResponse(Call<InforBean> call, Response<InforBean> response) { Log.i(TAG, "onResponse: " + response.body().toString()); if (response.isSuccessful()) { llInfo.setVisibility(View.VISIBLE); //对数据进行赋值 mTvArea.setText(response.body().result.area); mTvSex.setText(response.body().result.sex); mTvBirthday.setText(response.body().result.birthday); } else { llInfo.setVisibility(View.GONE); } } @Override public void onFailure(Call<InforBean> call, Throwable t) { Log.i(TAG, "onFailure: " + t.toString()); } }); } }</span>
标签:
原文地址:http://blog.csdn.net/wtoria/article/details/51925469