首页 > 解决方案 > 在改造 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
        );

无论如何,我是改装新手,有什么帮助吗?谢谢

标签: androidjsongsonretrofitretrofit2

解决方案


推荐阅读