2016-02-29 90 views
3

喜發送表單數據我有一個JSON發送到服務器(POST METHORD){"country":"india","devicetype":"android"}它是在像這樣JSON的關鍵表單數據模型 是數據即在服務器一樣接受它如何retrofit2的Android

​​正在使用改裝我使用多部這樣的

@Multipart 
@POST("initiate") 
@Headers({ 
     "Content-Type: application/json", 
     "Cache-Control: no-cache" 
}) 
Call<UserInfoServerResponse> getUserInfoRequest(@Part(value="data") UserInfo mUserInfo); 

這裏的UserInfo是JSON,但我得到之後無法從服務器消息我用FormUrlEncoded methord

@FormUrlEncoded 
@POST("initiate") 
@Headers({ 
     "Content-Type: application/json", 
     "Cache-Control: no-cache" 
}) 
Call<UserInfoServerResponse> getUserInfoRequest(@Field(value="data",encoded = false) String mUserInfo); 

它放出來,也從服務器相同的失敗的結果,但向服務器發送的數據是在甲

data=%7B%22country%22%3A%22india%22%2C%22devicetype%22%3A%22%22%7D 

UserInfo.class

public class UserInfo { 


public String country; 

public String devicetype; 

public UserInfo(String country,String devicetype) { 

    this.country=country; 

    this.devicetype=devicetype; 
} 
} 

我的適配器類

RemoteRetrofitInterfaces mService; 
    Retrofit mRetrofit; 
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
     interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 

     OkHttpClient client = new OkHttpClient.Builder() 
       .connectTimeout(20, TimeUnit.SECONDS) 
       .writeTimeout(20, TimeUnit.SECONDS) 
       .readTimeout(30, TimeUnit.SECONDS).addInterceptor(interceptor) 
       .build(); 

     mRetrofit = new Retrofit.Builder() 
       .baseUrl(AppConstant.HOST).addConverterFactory(GsonConverterFactory.create()) 
       .client(client) 
       .build(); 
     mService = mRetrofit.create(RemoteRetrofitInterfaces.class); 

    Call<UserInfoServerResponse> api = mService.getUserInfoRequest(new Gson().toJson(mUserInfo)); 

     api.enqueue(new Callback<UserInfoServerResponse>() { 
      @Override 
      public void onResponse(Call<UserInfoServerResponse> responseCall, Response<UserInfoServerResponse> response) { 

       if (response.body().status != null) { 
        if (response.body().status.equals("success")) { 
         Log.d(TAG, "success---"); 
        } 

       } else { 
        Log.d(TAG, "Failed---"); 

       } 


      } 

      @Override 
      public void onFailure(Call<UserInfoServerResponse> responseCall, Throwable t) { 
       t.printStackTrace(); 
      } 


     }); 

所以我怎麼能發送json到服務器使用改造成功我走過了retofit document並按照幾個步驟,但我沒有得到任何結果。任何一個可以幫我在這

謝謝

+0

安置自己的'UserInfo'類 –

+0

嘗試添加 「接受:應用/ JSON」 的標題。 –

+0

你的Retrofit RestAdapter代碼在哪裏? –

回答

6

終於讓我找到了解決辦法希望這將有助於其他一些

我實現的解決方案通過使用FieldMap

改造的

@POST("initiate") 
@FormUrlEncoded 

Call<UserInfoServerResponse> getUserInfoRequest(@FieldMap Map<String,String> params); 

,並在休息適配器區間i從字符串改變請求數據的HashMap形式像以下

Log.d(TAG, "sendUserInfo called"); 
UserInfo mInfo=new UserInfo("countyname","android"); 
     String request=new Gson().toJson(mUserInfo); 

     //Here the json data is add to a hash map with key data 
     Map<String,String> params = new HashMap<String, String>(); 
     params.put("data", request); 


     Call<UserInfoServerResponse> api = mService.getUserInfoRequest(params); 


     api.enqueue(new Callback<UserInfoServerResponse>() { 
      @Override 
      public void onResponse(Call<UserInfoServerResponse> responseCall, Response<UserInfoServerResponse> response) { 

       if (response.body().status != null) { 
        if (response.body().status.equals("success")) { 
         Log.d(TAG, "success---" + response.body()); 
        } 

       } else { 
        Log.d(TAG, "Failed---"); 

       } 


      } 

      @Override 
      public void onFailure(Call<UserInfoServerResponse> responseCall, Throwable t) { 
       t.printStackTrace(); 
      } 


     }); 

basilcally我有什麼用途是@FormUrlEncoded爲形式的數據和@FieldMap到把我的請求json作爲一個關鍵值。我得到的解決方案按照這種方法,希望這將幫助一些一個:)

0

上述解決方案的工作,但使用麻煩,更好的解決方案將使用一個轉換器,用於@Multipart FORMDATA

請使用婁代碼與@Multipart繼續FORMDATA 這是因爲

"" is added to your posting strings

import java.io.IOException; 
import java.lang.annotation.Annotation; 
import java.lang.reflect.Type; 

import okhttp3.MediaType; 
import okhttp3.RequestBody; 
import okhttp3.ResponseBody; 
import retrofit2.Converter; 
import retrofit2.Retrofit; 

/** 
* Created by kural on 10/27/17. 
*/ 

public class StringConverterFactory extends Converter.Factory { 
private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain"); 

public static StringConverterFactory create() { 
     return new StringConverterFactory(); 
     } 

@Override 
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { 
     if (String.class.equals(type)) { 
     return new Converter<ResponseBody, String>() { 

@Override 
public String convert(ResponseBody value) throws IOException { 
     return value.string(); 
     } 
     }; 
     } 
     return null; 
     } 

@Override 
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { 
    if (String.class.equals(type)) { 
     return new Converter<String, RequestBody>() { 
      @Override 
      public RequestBody convert(String value) throws IOException { 
       return RequestBody.create(MEDIA_TYPE, value); 
      } 
     }; 
    } 

    return null; 

} 
} 

,並在您的客戶端改造加入這一行

.addConverterFactory(StringConverterFactory。創建())

public class RetroFitClient { 
    private static Retrofit retrofit = null; 

    public static Retrofit getClient(String baseUrl) { 
     if (retrofit==null) { 
      HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
      interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
      OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); 

      /*retrofit = new Retrofit.Builder() 
        .baseUrl(baseUrl) 
        .client(client) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build();*/ 
      retrofit = new Retrofit.Builder() 
        .baseUrl(baseUrl) 
        .client(client) 
        .addConverterFactory(StringConverterFactory.create()) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 


     } 
     return retrofit; 
    } 
相關問題