android - 在改造 2 上输入文件图片时出错,在 gson 上显示 IllegalStateException
问题描述
我正在尝试使用 Retrofit 2 将文件(图片)上传到服务器。我正在按照给出的教程进行操作,但我仍然遇到了一些错误。
这是错误响应: D/Failed: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
这是我的代码: Call call = RetrofitClient .getInstance().getApi().loginAndroid(username, password);
//Toast.makeText(LoginActivity.this, "Login Success", Toast.LENGTH_LONG).show();
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
LoginResponse loginResponse = response.body();
int Responsecode = response.code();
if(Responsecode != 200)
{
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
Log.d("login", String.valueOf(jObjError));
Toast.makeText(LoginActivity.this, jObjError.getString("message"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Toast.makeText(LoginActivity.this, "Email / Password Salah", Toast.LENGTH_LONG).show();
} else {
Log.d("response", response.body().toString());
SharedPrefManager.getInstance(LoginActivity.this)
.saveUser(loginResponse.getData());
Toast.makeText(LoginActivity.this, "Login Success", Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this, AdminHomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
//Log.d("shared",SharedPrefManager.getInstance(LoginActivity.this).getUser().getNama().toString());
}
这是我的模型:private int id;私有字符串用户名、名称、角色、分支;
public User(int id, String username, String nama, String role, String branch)
{
this.id = id;
this.username = username;
this.nama = nama;
this.role = role;
this.branch = branch;
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getNama() {
return nama;
}
public String getRole() {
return role;
}
public String getBranch() {
return branch;
}
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setNama(String nama) {
this.nama = nama;
}
public void setRole(String role) {
this.role = role;
}
public void setBranch(String branch) {
this.branch = branch;
}
这是我的 API 调用:
@Multipart
@POST("sparepart")
Call<SparepartResponse> addSparepart(
@Part("id") String id,
@Part("nama") String nama,
@Part("merk") String merk,
@Part("harga_beli") Integer hargaBeli,
@Part("harga_jual") Integer hargaJual,
@Part("stok") Integer stok,
@Part("stok_minimal") Integer stokMinimal,
@Part("penempatan") String penempatan,
@Part MultipartBody.Part gambar,
@Part("id_sparepart_type") Integer idSparepartType
);
无论如何,我是改装新手,有什么帮助吗?谢谢
解决方案
推荐阅读
- reactjs - 尝试导入错误:'persistor' 未从 './redux/store' 导出
- html - xmlns 在 XHTML 的 HTML 标记中做了什么
- testing - 我无法使用 tsung 工具解释来自控制台的消息
- wordpress - 帖子类型分类未显示在古腾堡
- javascript - 什么是 Webtorrent 中仅浏览器的方法错误?
- momentjs - moment.js 使用 dd-mm-yyyy 格式比较日期
- php - 将数据添加到广播队列 Laravel
- android - 记住我在 android studio 中的复选框
- amazon-web-services - 消息轮询实例 ID
- python - Python:计算每种类型的标题数量和平均评分