I'm trying to create the action post for a login application. OnClick submitBtn the application is closed. Regardless of the backend configuration, I did not expect this behavior. Can you please help me? Thanks in advance.
public class MainActivity extends AppCompatActivity {
private TextView mResponseTv;
private APIService mAPIService;
private Button submitBtn;
private EditText titleEt;
private EditText bodyEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("carrapato", "Unable to submit post to API.");
final EditText titleEt = (EditText) findViewById(R.id.editText);
final EditText bodyEt = (EditText) findViewById(R.id.editText2);
Button submitBtn = (Button) findViewById(R.id.button);
mResponseTv = (TextView) findViewById(R.id.tv_response);
mAPIService = ApiUtils.getAPIService();
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.v("capiabas", "Unable 234234234 to API.");
String title = titleEt.getText().toString().trim();
String body = bodyEt.getText().toString().trim();
if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(body)) {
Log.v("clips", "Urerertert to API.");
sendPost(title, body);
}
}
public void sendPost(String title, String body) {
Log.d("catapora", "Unable to submit post to API.");
mAPIService.savePost(title, body, 1).enqueue(new Callback<Post>() {
private static final String TAG = "MainActivity2" ;
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if (response.isSuccessful()) {
showResponse(response.body().toString());
Log.v("caixinha", "post submitted to API." + response.body().toString());
}
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
Log.e("bororo", "Unable to submit post to API.");
}
});
}
public void showResponse(String response) {
if (mResponseTv.getVisibility() == View.GONE) {
mResponseTv.setVisibility(View.VISIBLE);
}
mResponseTv.setText(response);
}
});
}
}
/
////////////////////////
public class ApiUtils {
private ApiUtils() {}
public static final String BASE_URL = "http://192.168.xxx";
public static APIService getAPIService() {
Log.e("carrapato", "bbbb999999.");
return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}
}
/
////////////////////////////
public interface APIService {
@POST("")
@FormUrlEncoded
Call<Post> savePost(@Field("title") String title,
@Field("body") String body,
@Field("userId") long userId);
}
/
/////////////////
public class Post {
@SerializedName("title")
@Expose
private String title;
@SerializedName("body")
@Expose
private String body;
@SerializedName("userId")
@Expose
private Integer userId;
@SerializedName("id")
@Expose
private Integer id;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "Post{" +
"title='" + title + '\'' +
", body='" + body + '\'' +
", userId=" + userId +
", id=" + id +
'}';
}
}