2017-06-21 41 views
0

我想使用此URL来获取Android中的数据。我要Retrofit库为POSTGET方法。
我的URLJSON Link如何在Android中使用POST方法进行改造

我想POST输入国家半径关键此URL和GET数据。

如何可以写为上述请求的代码,如这一行:

@POST("?json={input}") 
Call<Response> getResponseWithID(@Path("input") String input); 

如何能我吗?请帮帮我。

+1

所以你要发布这个请求一些JSON对象O API? –

+0

我想发送输入,国家,半径和密钥到这个URL来获取数据。我怎么能呢? –

回答

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

+0

谢谢我的朋友,你能用我的上面的代码发给我代码吗?请 –

+0

你能帮我做我的朋友吗?请 –

+0

@DrKoKo完成,请检查 –

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); 


    } 

} 

后调用此位置为folow中

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

请用我的上面的代码编辑@POST(BasePosition.API_LOGIN)。请 –

+0

你能帮我我的朋友吗?请 –

+0

请用我的上述要求更新您的代码,并需要参数 –