我正在尝试将数据发布到数据库中,但我无法发布数据。我已经为此写了网络服务,但在Android应用程序中收到错误
public class RegisterActivity extends AppCompatActivity {
private EditText mEditTextName;
private EditText mEditTextEmail;
private EditText mEditTextPassword;
private EditText mEditTextAddress;
private Button mButtonRegisterUser;
private TextView mResponseTextView;
APIService mAPIService;
private static final String TAG = "RegisterActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mEditTextName = (EditText) findViewById(R.id.editName);
mEditTextEmail = (EditText) findViewById(R.id.editEmail);
mEditTextAddress = (EditText) findViewById(R.id.editAddress);
mEditTextPassword = (EditText) findViewById(R.id.editPassword);
mButtonRegisterUser = (Button) findViewById(R.id.buttonRegisterUser);
mResponseTextView = (TextView) findViewById(R.id.textResponse);
mAPIService = ApiUtils.getAPIService();
mButtonRegisterUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String lName = mEditTextName.getText().toString();
String lEmail = mEditTextEmail.getText().toString();
String lAddress = mEditTextAddress.getText().toString();
String lPassword = mEditTextPassword.getText().toString();
sendPost(lName, lAddress, lEmail, lPassword);
}
});
}
private void sendPost(String lName, String lAddress, String lEmail, String lPassword) {
mAPIService.registerPost(lName, lAddress, lEmail, lPassword).enqueue(new Callback<POST>() {
@Override
public void onResponse(Call<POST> call, Response<POST> response) {
if (response.isSuccessful()) {
showResponse(response.body().toString());
Log.d(TAG, "onResponse: " + response.body().toString());
}
}
@Override
public void onFailure(Call<POST> call, Throwable t) {
Log.e(TAG, "onFailure: Unable to post data");
t.printStackTrace();
}
});
}
private void showResponse(String response) {
if (mResponseTextView.getVisibility() == View.GONE) {
mResponseTextView.setVisibility(View.VISIBLE);
}
mResponseTextView.setText(response);
}
}
我的活动代码。我已经提供了互联网访问权限
public class RetrofitClient {
private static Retrofit sRetrofit = null;
public static Retrofit getClient(String baseURL) {
if (sRetrofit == null) {
sRetrofit = new Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return sRetrofit;
}
}
描述基本网址的APIUtils
public class ApiUtils {
private ApiUtils() {
}
public static final String BASE_URL = "http://localhost/services/webapi/registerService/";
public static APIService getAPIService() {
return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}
}
用于定义方法的API服务
public interface APIService {
@POST("register")
@FormUrlEncoded
Call<POST> registerPost(@Field("name") String name,
@Field("email") String email,
@Field("password") String password,
@Field("address") String address);
}
答案 0 :(得分:0)
如果您的回复是
{
"message": "Successful"
}
然后创建一个类
public class RegisterResponse {
@SerializedName("message")
private String message;
public String getMessage() {
return message;
}
}
在您的界面类中进行以下更改
public interface APIService {
@POST("register")
@FormUrlEncoded
Call<RegisterResponse> registerPost(@Field("name") String name,
@Field("email") String email,
@Field("password") String password,
@Field("address") String address);
}
调用函数时
mAPIService.registerPost(lName, lAddress, lEmail, lPassword).enqueue(new Callback<RegisterResponse>() {
@Override
public void onResponse(Call<RegisterResponse> call, Response<RegisterResponse> response) {
if (response.isSuccessful()) {
showResponse(response.body().getMessage());
Log.d(TAG, "onResponse: " + response.body().getMessage());
}
}
@Override
public void onFailure(Call<RegisterResponse> call, Throwable t) {
Log.e(TAG, "onFailure: Unable to post data");
t.printStackTrace();
}
});