Android 安卓通过改造向服务器发送字符串

Android 安卓通过改造向服务器发送字符串,android,retrofit2,Android,Retrofit2,我想用改型2将字符串从应用程序发送到服务器,并获取返回值。有什么问题? 但它不起作用 Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); RatingApiService retrofitService

我想用改型2将字符串从应用程序发送到服务器,并获取返回值。有什么问题? 但它不起作用

Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            RatingApiService retrofitService=retrofit.create(RatingApiService.class);

            Call<String> call = retrofitService.registration("saeed","ali");
            call.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    if(response!=null){
                        Log.i("upload","is success:" +response.body());
                    }else{
                        Log.i("upload","response is null");
                    }
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Log.i("upload","onFailure: "+t.getMessage());
                }
            });
Reformation-Reformation=new-Reformation.Builder().baseUrl(基本URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RatingApiService REFRANTIONSERVICE=REFRANTIONG.create(RatingApiService.class);
Call Call=service.registration(“saeed”、“ali”);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(响应!=null){
Log.i(“upload”,“is success:+response.body());
}否则{
Log.i(“上传”,“响应为空”);
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.i(“上传”,“onFailure:+t.getMessage());
}
});
接口:

public interface RatingApiService {
    @FormUrlEncoded
    @POST("android/add2/{email}{password}")
    Call<String> registration(@Path("email") String email, @Path("password") String password);
}
公共接口分级服务{
@FormUrlEncoded
@POST(“安卓/add2/{email}{password}”)
呼叫注册(@Path(“email”)字符串email、@Path(“密码”)字符串password);
}

您需要向方法添加
@Body
注释,如下所示

@POST("users/new")
Call<User> createUser(@Body User user);
@POST(“用户/新”)
调用createUser(@Body User);
@Body
的内容作为请求正文发送到服务器。
如果要向服务器发送电子邮件和密码,应创建包含此类数据的对象,并在
@Body
注释中使用该对象。

而不是使用参数传递数据;将它放入一个req.body中,然后发送它。通过这样做,您可以绕过URL字符串限制,并且代码更具组织性

public interface RatingApiService {
  @Headers("Content-Type: application/json") //Must be set to application/json so req.body can be read.
  @POST("android")
  Call<String> registration(@Body UserRegistrationRequest body);
}
然后是最后一步。这就是将对象转换为JSON对象并将其发送到服务器的方式

       UserRegistrationRequest userRegistrationRequest = new UserRegistrationRequest(
                RegisterNameFragment.sFirstName, RegisterNameFragment.sLastName, RegisterEmailFragment.sEmail,
                RegisterPasswordFragment.sPassword, RegisterAgeFragment.sAge);

            Call<Void> call = retrofit.insertUserRegistration(userRegistrationRequest);
            call.enqueue(new Callback<UserRegistration>() {
                @Override
                public void onResponse(Call<UserRegistration> call, Response<UserRegistration> response) {
                    Log.d("blue", "Data is sent");

                }
                @Override
                public void onFailure(Call<UserRegistration> call, Throwable t) {
                    Log.d("blue", "fail");
                }
            });
UserRegistrationRequest UserRegistrationRequest=新的UserRegistrationRequest(
RegisterNameFragment.sFirstName、RegisterNameFragment.sLastName、registerMailFragment.sEmail、,
RegisterPasswordFragment.sPassword、RegisterAgeFragment.sAge);
Call Call=reformation.insertUserRegistration(用户注册请求);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
Log.d(“蓝色”,“数据已发送”);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
日志d(“蓝色”、“失败”);
}
});

没有数据发送到服务器,但数据是空的,当我使用$\u POST nothing found时,如何在php服务器中获得结果。感谢android监视器显示“失败”或“数据已发送”?数据发送到服务器并显示“数据已发送”。但日志中显示“内部服务器错误”。在php网站中,我有一个cakephp框架,只需保存通过$\u POST发送的任何东西,它是空的。谢谢,代码正在运行!内部服务器错误或错误500表示您的服务器有问题。尝试console.logging您的POST请求的正文。使用PHP中的
$this->request
而不是
$\u POST
检查正文内容。
       UserRegistrationRequest userRegistrationRequest = new UserRegistrationRequest(
                RegisterNameFragment.sFirstName, RegisterNameFragment.sLastName, RegisterEmailFragment.sEmail,
                RegisterPasswordFragment.sPassword, RegisterAgeFragment.sAge);

            Call<Void> call = retrofit.insertUserRegistration(userRegistrationRequest);
            call.enqueue(new Callback<UserRegistration>() {
                @Override
                public void onResponse(Call<UserRegistration> call, Response<UserRegistration> response) {
                    Log.d("blue", "Data is sent");

                }
                @Override
                public void onFailure(Call<UserRegistration> call, Throwable t) {
                    Log.d("blue", "fail");
                }
            });