本篇是Retrofit Url 配置 http://blog.csdn.net/jdsjlzx/article/details/51588605博客的补充。
GET请求
多个参数在URL问号之后,且个数不确定
http://api.stay4it.com/News?newsId=1&type=类型1…
http://api.stay4it.com/News?newsId={资讯id}&type={类型}…
@GET("News")
Call<NewsBean> getItem(@QueryMap Map<String, String> map);
- 1
- 2
或者:
@GET("News")
Call<NewsBean> getItem(
@Query("newsId") String newsId,
@QueryMap Map<String, String> map);
- 1
- 2
- 3
- 4
POST请求
- 需要补全URL,post的数据只有一条reason
http://102.10.10.132/api/Comments/1
http://102.10.10.132/api/Comments/{newsId}
@FormUrlEncoded
@POST("Comments/{newsId}")
Call<Comment> reportComment(
@Path("newsId") String commentId,
@Field("reason") String reason);
- 1
- 2
- 3
- 4
- 5
- 需要补全URL,问号后加入access_token,post的数据只有一条reason
http://102.10.10.132/api/Comments/1?access_token=1234123
http://102.10.10.132/api/Comments/{newsId}?access_token={access_token}
@FormUrlEncoded
@POST("Comments/{newsId}")
Call<Comment> reportComment(
@Path("newsId") String commentId,
@Query("access_token") String access_token,
@Field("reason") String reason);
- 1
- 2
- 3
- 4
- 5
- 6
- 需要补全URL,问号后加入access_token,post一个body(对象)
http://102.10.10.132/api/Comments/1?access_token=1234123
http://102.10.10.132/api/Comments/{newsId}?access_token={access_token}
@POST("Comments/{newsId}")
Call<Comment> reportComment(
@Path("newsId") String commentId,
@Query("access_token") String access_token,
@Body CommentBean bean);
- 1
- 2
- 3
- 4
- 5
DELETE
需要补全URL
http://102.10.10.132/api/Comments/1
http://102.10.10.132/api/Comments/{newsId}
{access_token}
@DELETE("Comments/{commentId}")
Call<ResponseBody> deleteNewsCommentFromAccount(
@Path("accountId") String accountId);
- 1
- 2
- 3
需要补全URL,问号后加入access_token
http://102.10.10.132/api/Comments/1?access_token=1234123
http://102.10.10.132/api/Comments/{newsId}?access_token={access_token}
@DELETE("Comments/{commentId}")
Call<ResponseBody> deleteNewsCommentFromAccount(
@Path("accountId") String accountId,
@Query("access_token") String access_token);
- 1
- 2
- 3
- 4
PUT(这个请求很少用到,例子就写一个)
http://102.10.10.132/api/Accounts/1
http://102.10.10.132/api/Accounts/{accountId}
@PUT("Accounts/{accountId}")
Call<ExtrasBean> updateExtras(
@Path("accountId") String accountId,
@Query("access_token") String access_token,
@Body ExtrasBean bean);
- 1
- 2
- 3
- 4
- 5
总结
@Path:所有在网址中的参数(URL的问号前面),如:
http://102.10.10.132/api/Accounts/{accountId}
@Query:URL问号后面的参数,如:
http://102.10.10.132/api/Comments?access_token={access_token}
@QueryMap:相当于多个@Query
@Field:用于POST请求,提交单个数据
@Body:相当于多个@Field,以对象的形式提交
TIps
- Tip1
使用@Field时记得添加@FormUrlEncoded - Tip2
若需要重新定义接口地址,可以使用@Url,将地址以参数的形式传入即可。如
@GET
Call<List<Activity>> getActivityList(
@Url String url,
@QueryMap Map<String, String> map);
- 1
- 2
- 3
- 4
Call<List<Activity>> call = service.getActivityList(
"http://115.159.198.162:3001/api/ActivitySubjects", map);
- 1
- 2
参考简书:http://www.jianshu.com/p/7687365aa946
还有@FieldMap
如匿名发表新评论:
接口地址为: /posts/create
HTTP请求方式: POST
请求示例为:
Request URL: http://api.duoshuo.com/posts/create.json
Request Method: POST
Post Data: short_name=official&author_email=jp.chenyang%40gmail.com&author_name=Perchouli&thread_id=1152923703638301959&author_url=http%3A%2F%2Fduoshuo.com&message=匿名发表新评论
1.Field方式实现
@FormUrlEncoded
@POST("/posts/create.json")
Call<CommitResult> createCommit(@Field("secret") String secret,
@Field("short_name") String shortName,
@Field("author_email") String authorEmail,
@Field("author_name") String authorName,
@Field("thread_key") String threadKey,
@Field("author_url") String author_url,
@Field("message") String message);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2.Field Map实现方式
@FormUrlEncoded
@POST("/posts/create.json")
Call<CommitResult> createCommit(@FieldMap Map<String, String> map);
- 1
- 2
- 3
获取Map方式如下:
public class CommitParam {
private String short_name;
private String author_email;
private String author_name;
private String thread_id;
private String author_url;
private String message;
public String getShort_name() {
return short_name;
}
public void setShort_name(String short_name) {
this.short_name = short_name;
}
public String getAuthor_email() {
return author_email;
}
public void setAuthor_email(String author_email) {
this.author_email = author_email;
}
public String getAuthor_name() {
return author_name;
}
public void setAuthor_name(String author_name) {
this.author_name = author_name;
}
public String getThread_id() {
return thread_id;
}
public void setThread_id(String thread_id) {
this.thread_id = thread_id;
}
public String getAuthor_url() {
return author_url;
}
public void setAuthor_url(String author_url) {
this.author_url = author_url;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Map<String, String> createCommitParams(){
Map<String, String> params = new HashMap<>();
params.put("short_name", short_name);
params.put("author_email", author_email);
params.put("author_name", author_name);
params.put("thread_id", thread_id);
params.put("author_url", author_url);
params.put("message", message);
return params;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
具体使用:
package net.devwiki.retrofitdemo.duoshuo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import net.devwiki.retrofitdemo.R;
import java.util.Map;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class DuoShuoActivity extends AppCompatActivity {
private static final String TAG = DuoShuoActivity.class.getSimpleName();
private DuoShuoApi duoShuoApi;
private CommitParam commitParam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duo_shuo);
ButterKnife.bind(this);
duoShuoApi = DuoShuoApi.getApi();
commitParam = new CommitParam();
commitParam.setSecret("key");
commitParam.setAuthor_email("[email protected]");
commitParam.setAuthor_name("xxx");
commitParam.setAuthor_url("http://www.devwiki.net");
commitParam.setShort_name("devwiki");
commitParam.setThread_key("2016/03/13/Gradle-Get-SVN-Version-Code/");
commitParam.setMessage("test commit!!!");
}
@OnClick({R.id.create_commit_single, R.id.create_commit_map})
public void onClick(View view) {
switch (view.getId()) {
case R.id.create_commit_single:
createSingle();
break;
case R.id.create_commit_map:
createMap();
break;
}
}
private Callback<CommitResult> callback = new Callback<CommitResult>() {
@Override
public void onResponse(Call<CommitResult> call, Response<CommitResult> response) {
if (response.isSuccessful()){
Log.i(TAG, "success!!!");
Log.i(TAG, "---" + response.body().toString());
} else {
Log.e(TAG, "+++" + response.message());
}
}
@Override
public void onFailure(Call<CommitResult> call, Throwable t) {
Log.e(TAG, "***" + t.getMessage());
}
};
private void createSingle(){
Call<CommitResult> call = duoShuoApi.getService().createCommit(
commitParam.getSecret(),
commitParam.getShort_name(),
commitParam.getAuthor_email(),
commitParam.getAuthor_name(),
commitParam.getThread_key(),
commitParam.getAuthor_url(),
commitParam.getMessage());
call.enqueue(callback);
}
private void createMap(){
Map map = commitParam.createCommitParams();
Call<CommitResult> call = duoShuoApi.getService().createCommit(map);
call.enqueue(callback);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
项目地址在此: Dev-Wiki/RetrofitDemo
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net