2016-08-30 2 views
0

'@Bodyパラメータはフォームまたは複数部分のエンコーディングで使用できません' FormUrlEncodedでDTOを送信すると、@ FormUrlEncodedを削除すると自然に が削除されます。エラーが発生する FormUrlEncoded用の私のサーバー構成FormUrlEncodedでDTOを送信するとRerofit2投稿エラーが発生する

FormUrlEncodedでDTOを送信したいのですが、どうすればいいですか?このような

RestAPI.java

public interface RestAPI { 
    interface Join { 
     @FormUrlEncoded 
     @POST("/api/users") 
     Call<Res> createTask(@Body User user); 
    } 
    Retrofit RETROFIT = new Retrofit.Builder().baseUrl(Config.ServerUrl).addConverterFactory(GsonConverterFactory.create()).build(); 
} 


public class User { 
String username; 
String password; 


public String getUsername() { 
    return username; 
} 

public void setUsername(String username) { 
    this.username = username; 
} 

public String getPassword() { 
    return password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 
} 

Res.java

public class Res { 

private Boolean response; 
private String msg; 

public Boolean getResponse() { 
    return response; 
} 

public void setResponse(Boolean response) { 
    this.response = response; 
} 

public String getMsg() { 
    return msg; 
} 

public void setMsg(String msg) { 
    this.msg = msg; 
} 
} 

Main.java

user = new User(); 
user.setUsername("TEST"); 
user.setPassword("1234"); 
    RestAPI.Join join = RestAPI.RETROFIT.create(RestAPI.Join.class); 
    Call<Res> resCall = join.createTask(user); 
    resCall.enqueue(new Callback<Res>() { 
    @Override 
    public void onResponse(Call<Res> call, Response<Res> response) { 
    System.out.println(response.body().getResponse()); 
    } 

    @Override 
    public void onFailure(Call<Res> call, Throwable t) { 

    } 
    }); 

答えて

0

私が使用している改修:

RetrofitAPI.class

public interface RetrofitAPI 
{ 
    @POST("/your/server/doSomething") 
    Call<ReturnData> doSomething(@Body Param param); 
} 

ServiceGenerator.class

private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 

private static Retrofit retrofit = null; 
private static RetrofitAPI retrofitAPI = null; 
private static Retrofit.Builder builder = new Retrofit.Builder().baseUrl(Constant.BASE_URL).addConverterFactory(GsonConverterFactory.create()); 

public static Retrofit getRetrofit() 
{ 
    if(retrofit == null) 
     retrofit = builder.client(httpClient.build()).build(); 
    return retrofit; 
} 
private static <S> S createService(Class<S> serviceClass) { 
    return getRetrofit().create(serviceClass); 
} 
public static RetrofitAPI getRetrofitAPI() 
{ 
    if(retrofitAPI == null) 
     retrofitAPI = createService(RetrofitAPI.class); 
    return retrofitAPI; 
} 

実際のコード

public static void doSomethingViaServer(Param param, Callback<ReturnData> callback) 
{ 
    if(param == null) 
     return; 

    RetrofitAPI api = ServiceGenerator.getRetrofitAPI(); 
    Call<ReturnData> call = api.doSomething(param); 
    call.enqueue(callback); 
} 

・ホープ、このことができます。

+0

Thansk、問題、サーバーの問題、サーバー入力コード 'app.use(bodyParser())'を解決しました。サーバーがフォームデータをポストできるようになりましたが、 –

関連する問題