2016-02-29 77 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; 
    } 
相关问题