[英]How to use Retrofit in Android POST method along with body?
[英]How to use POST method in retrofit in Android
我想使用此URL來獲取Android中的數據。 我用於POST
和GET
方法的Retrofit
庫。
我的網址 : JSON鏈接
我想要這個URL和GET
數據的POST
輸入 , 國家 , 半徑和密鑰 。
如何為上述請求編寫代碼,例如:
@POST("?json={input}")
Call<Response> getResponseWithID(@Path("input") String input);
我怎么樣? 請幫我。
您需要指定API方法,如下所示:
@GET("api/place/autocomplete/json")
Call<Response> getResponseWithID(Query("input") String input, Query("language") String language, @QueryMap Map<String, String> components);
在那里,您需要為components
映射提供您需要的值:
Map<String, String> components = new HashMap<>();
components.put("country", country);
components.put("location", location);
components.put("radius", radius);
components.put("key", key);
而且你不需要POST方法,只需要簡單的GET
在這里發送發送字段或正文
package nuvuw.com.nuvuw.utils;
import java.util.concurrent.TimeUnit;
import nuvuw.com.nuvuw.models.AcceptTaskModel;
import nuvuw.com.nuvuw.models.AddMyPartnerModel;
import nuvuw.com.nuvuw.models.AddPartnersModel;
import nuvuw.com.nuvuw.models.CardDetailModel;
import nuvuw.com.nuvuw.models.CatagoryModel;
import nuvuw.com.nuvuw.models.ChangePasswordModel;
import nuvuw.com.nuvuw.models.CollaborationFeedBackModel;
import nuvuw.com.nuvuw.models.CollaborationModel;
import nuvuw.com.nuvuw.models.CreateEventModel;
import nuvuw.com.nuvuw.models.DeleteImageModel;
import nuvuw.com.nuvuw.models.DeleteProjectImageModel;
import nuvuw.com.nuvuw.models.DiscoverModel;
import nuvuw.com.nuvuw.models.FolderCreateModel;
import nuvuw.com.nuvuw.models.FolderModel;
import nuvuw.com.nuvuw.models.FolderSaveModel;
import nuvuw.com.nuvuw.models.LicenseModel;
import nuvuw.com.nuvuw.models.MailSendModel;
import nuvuw.com.nuvuw.models.Profile_info_model;
import nuvuw.com.nuvuw.models.RegisterModel;
import nuvuw.com.nuvuw.models.ReviewModel;
import nuvuw.com.nuvuw.models.SaveDesignImageModel;
import nuvuw.com.nuvuw.models.SaveProjectDecideModel;
import nuvuw.com.nuvuw.models.SaveUserPartnerModel;
import nuvuw.com.nuvuw.models.SendInviteMailModel;
import nuvuw.com.nuvuw.models.SingleSignInModel;
import nuvuw.com.nuvuw.models.UpdateProjectInfoModel;
import nuvuw.com.nuvuw.models.UsersModel;
import nuvuw.com.nuvuw.models.WriteError;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.http.Streaming;
import retrofit2.http.Url;
public class ApiServiceFactory {
private static ApiService apiService_instance;
static final int CONNECT_TIMEOUT_MILLIS = 20 * 1000;
static final int READ_TIMEOUT_MILLIS = 20 * 1000;
public static ApiService getApiService() {
if (apiService_instance == null) {
synchronized (ApiServiceFactory.class) {
if (apiService_instance == null) {
apiService_instance = getRetrofit().create(ApiService.class);
}
}
}
return apiService_instance;
}
private static Retrofit getRetrofit() {
OkHttpClient client = new OkHttpClient();
client.connectTimeoutMillis();
client.readTimeoutMillis();
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.build();
return new Retrofit.Builder()
.baseUrl(BasePosition.BASE_API)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
// return new Retrofit.Builder()
// .client(client)
// .baseUrl(BasePosition.BASE_API)
// .addConverterFactory(GsonConverterFactory.create())
// .build();
}
public interface ApiService {
//login
@FormUrlEncoded
@POST(BasePosition.API_LOGIN)
Call<ResponseBody> getLoginUserIdentify(@Field("username") String userName, @Field("password") String passWord, @Field("grant_type") String grantType);
@POST()
Call<ResponseBody> forgotMail(@Url String userMail, @Body ChangePasswordModel changePasswordModel);
@GET
Call<ResponseBody> getUserNotification(@Url String url_getnotification, @Header("Content-Type") String content, @Header("Authorization") String token);
}
}
在這里發布呼叫位置如下
ApiServiceFactory.ApiService apiService = ApiServiceFactory.getApiService();
apiService.getLoginUserIdentify(userName, passWord, "password").enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//ur operation
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
dialog.dismiss();
//
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.