如何在Android中改造中使用POST方法

时间:2017-06-21 07:30:06

标签: android json retrofit retrofit2

我想在Android中使用此网址获取数据。我对<{1}}和Retrofit方法的 POST 库感兴趣。
我的网址JSON Link

我想 GET 输入国家半径适用于此网址和POST数据。

如何编写上述请求的代码,例如:

GET

我怎么样?请帮帮我。

2 个答案:

答案 0 :(得分:0)

您需要按如下方式指定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

答案 1 :(得分:0)

在此处发送字段或正文提交

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();
      //
    }
});