日志目录错误
2023-02-25 15:15:09.576 18889-18889/exampleapp.com.notes D/AddNoteActivity: Try Catch Block Failed.... Attempt to invoke virtual method 'int tweakup.ru.notes.models.DefaultResponse.getCode()' on a null object reference
- 请注意 * 从 Postman 发送数据完美的作品,以及检查数据库后,每一个单一的职位。我安装在我的移动的上的应用程序,仍然得到同样的Logcat错误。改装配置是伟大的,但我找不到任何东西来帮助我解决这个错误!
客户端API
public class ApiClient {
public static Retrofit RETROFIT = null;
public static Retrofit getClient() {
if(RETROFIT == null) {
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
Gson gson = new GsonBuilder().create();
RETROFIT = new Retrofit.Builder()
.baseUrl("http://192.555.3.122/notes/note/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson)).build();
}
return RETROFIT;
}
}
API接口
public interface ApiInterface {
@FormUrlEncoded
@POST("create")
Call<DefaultResponse> apiNoteAdd(
@Field("title") String title,
@Field("description") String description
);
}
添加备注活动
public class AddNoteActivity extends AppCompatActivity {
private static final String LOG_TAG = AddNoteActivity.class.getSimpleName();
private ApiInterface apiInterface;
EditText mEditTextTitle;
EditText mEditTextDescription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_note);
findViews();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save_note:
if (checkInputs()) {
addNote();
}
return true;
}
return super.onOptionsItemSelected(item);
}
private boolean checkInputs() {
boolean status = true;
if (TextUtils.isEmpty(mEditTextTitle.getText().toString().trim())) {
mEditTextTitle.requestFocus();
mEditTextTitle.setError("Note title is required!");
status = false;
}
if (TextUtils.isEmpty(mEditTextDescription.getText().toString().trim())) {
mEditTextDescription.requestFocus();
mEditTextDescription.setError("Note description is required!");
status = false;
}
return status;
}
private void addNote() {
String title = mEditTextTitle.getText().toString().trim();
String message = mEditTextDescription.getText().toString().trim();
Retrofit retrofit = ApiClient.getClient();
apiInterface = retrofit.create(ApiInterface.class);
apiInterface.apiNoteAdd(title, message).enqueue(new Callback<DefaultResponse>() {
@Override
public void onResponse(Call<DefaultResponse> call, Response<DefaultResponse> response) {
try {
if (response.body().getCode() == 200) {
Toast.makeText(AddNoteActivity.this, "Note created!", Toast.LENGTH_SHORT).show();
} else if (response.body().getCode() == 400) {
Toast.makeText(AddNoteActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AddNoteActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.d(LOG_TAG, "Try Catch Block Failed.... " + e.getLocalizedMessage());
}
}
@Override
public void onFailure(Call<DefaultResponse> call, Throwable t) {
Log.d(LOG_TAG, "API Failed.... " + t.getLocalizedMessage());
}
});
}
private void findViews() {
mEditTextTitle = findViewById(R.id.edittext_title);
mEditTextDescription = findViewById(R.id.edittext_description);
}
}
清单中的Internet权限
<uses-permission android:name="android.permission.INTERNET" />
默认响应
public class DefaultResponse {
public int code;
public String message;
public DefaultResponse(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
** Postman 回应**
{
"code": 200,
"message": "Note successfully added",
"result": 11
}
1条答案
按热度按时间o7jaxewo1#
最后我设法解决了我的问题。2这是我的Logcat错误
带有消息“方法不允许”的Retrofit响应代码405导致了这个问题。因为我错误地注解了我的方法,
注解必须与API匹配,并更改为