我正在尝试使用Retrofit 2将文件(图片)上传到服务器。我正在按照给出的教程进行操作,但是仍然出现一些错误。
这是错误响应: D /失败:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但位于第1行第1列的路径$
这是我的代码: 通话= RetrofitClient .getInstance()。getApi()。loginAndroid(用户名,密码);
//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());
}
这是我的模型: 私人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
);
无论如何我还是新手,有什么帮助吗?谢谢