(使用JsonReader.setLenient(true)在第1行第1列路径$接受格式错误的JSON)&(预期为BEGIN_OBJECT但在第1行第1列为STRING)

我想使用Retrofit将表单发送到服务器,但是出现错误。

使用JsonReader.setLenient(true)在第1行第1列路径$处接受格式错误的JSON。

我试图像下面这样修复它:

Gson gson = new GsonBuilder()
        .setLenient()
        .create();

return new Retrofit.Builder()
        .baseUrl(AppConfig.BASE_URL)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

那么我有这个错误:

预期为BEGIN_OBJECT,但在第1行第1列路径$

我的php文件:

<?php

if($_SERVER['REQUEST_METHOD']=='POST') {

    $jenis = $_POST['jenis'];
    $tipe = $_POST['tipe'];
    $nominal = $_POST['nominal'];

    require_once("connect.php");

    $sql = "INSERT INTO keuangan (jenis,tipe,nominal) VALUES('$jenis','$tipe','$nominal')";

    if(mysqli_query($conn,$sql)) {

        $response["value"] = 1;
        $response["message"] = "Laporan berhasil Ditambahkan";
        echo json_encode($response);
    } 
    else {

        $response["value"] = 0;
        $response["message"] = "ERROR! | Laporan Gagal Ditambahkan";
        echo json_encode($response);
    }

   mysqli_close($conn);
 }
 else {

  $response["value"] = 0;
  $response["message"] = "ERROR,OUT OF METHOD";
  echo json_encode($response);
 }

我的界面API:

public interface RegisterAPI {

    @FormUrlEncoded
    @POST("insert.php")
    Call<Value> daftar (
        @Field("jenis") String jenis,@Field("tipe") String tipe,@Field("nominal") String nominal
    );

}

我的模特:

public class Value {

    String value;
    String message;

    public String getvalue() {
        return value;
    }

    public void setvalue(String value) {
        this.value = value;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

我的活动:

@OnClick(R.id.btn_Daftar) void daftar() {
        //membuat progress dialog
        progress = new ProgressDialog(this);
        progress.setCancelable(false);
        progress.setMessage("Loading ...");
        progress.show();

        //mengambil data dari edittext
        String jenis = et_jenis.getText().toString();
        String tipe = et_tipe.getText().toString();
        String nominal = et_nominal.getText().toString();

        int selectedId = radioGroup.getcheckedRadioButtonId();
        radioSexButton = (RadioButton) findViewById(selectedId);
        String sesi = radioSexButton.getText().toString();

//        ==FIRST ERROR==
//        Retrofit retrofit = new Retrofit().Builder()
//                .baseUrl(URL)
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();

//        ==SECOND ERROR==
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

            retrofit = new Retrofit.Builder()
                    .baseUrl(URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();

        RegisterAPI api = retrofit.create(RegisterAPI.class);
        Call<Value> call = api.daftar(jenis,nominal);
        call.enqueue(new Callback<Value>() {
            @Override
            public void onResponse(Call<Value> call,Response<Value> response) {
                String value = response.body().getvalue();
                String message = response.body().getMessage();
                progress.dismiss();
                if (value.equals("1")) {
                    Toast.makeText(Mainactivity.this,message,Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(Mainactivity.this,Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Value> call,Throwable t) {
                progress.dismiss();
                String message = t.getLocalizedMessage();
                Toast.makeText(Mainactivity.this,Toast.LENGTH_SHORT).show();
            }
        });

    }

每个建议都会很有帮助。谢谢

yangbin1by 回答:(使用JsonReader.setLenient(true)在第1行第1列路径$接受格式错误的JSON)&(预期为BEGIN_OBJECT但在第1行第1列为STRING)

预期对象类型为Value,但在API调用中得到了字符串

@FormUrlEncoded
@POST("insert.php")
Call<Value> daftar (
    @Field("jenis") String jenis,@Field("tipe") String tipe,@Field("nominal") String nominal
);

尝试从 Postman 用相同的值调用daftar(),然后检查输出。

本文链接:https://www.f2er.com/3149600.html

大家都在问