Android &引用;应为BEGIN_对象,但在第2行第1列路径$”处为字符串;改造中

Android &引用;应为BEGIN_对象,但在第2行第1列路径$”处为字符串;改造中,android,retrofit,Android,Retrofit,我想发送一个注册表格到服务器改造。首先我有一个错误: 使用JsonReader.setLenient(true)在第1行第1列路径$处接受格式错误的JSON 我正在尝试通过添加Gson来修复它。生成器如下: Gson gson = new GsonBuilder() .setLenient() .create(); return new Retrofit.Builder() .baseUrl(AppConfig.

我想发送一个注册表格到服务器改造。首先我有一个错误:

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

我正在尝试通过添加Gson来修复它。生成器如下:

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

    return new Retrofit.Builder()
            .baseUrl(AppConfig.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
{
"status": 1,
"message": "Successful"
}
Call<ResponseBody> call = getResponse.uploadFile(fileToUpload, filename, name, phone, email, password, username);
call.enqueue(new Callback<ResponseBody>() {
     @Override
     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
             try {
                // here you can what response you are getting.
                Log.d("JSON From Server", response.body().string());//convert reponse to string
             } catch (IOException e) {
                e.printStackTrace();
             }
      }
      @Override
      public void onFailure(Call<ResponseBody> call, Throwable t) {
          Log.i("error", t.getMessage());
      }
  });
现在,我有一个错误:

应为BEGIN_对象,但在第2行第1列路径处为字符串$

我的服务器响应如下:

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

    return new Retrofit.Builder()
            .baseUrl(AppConfig.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
{
"status": 1,
"message": "Successful"
}
Call<ResponseBody> call = getResponse.uploadFile(fileToUpload, filename, name, phone, email, password, username);
call.enqueue(new Callback<ResponseBody>() {
     @Override
     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
             try {
                // here you can what response you are getting.
                Log.d("JSON From Server", response.body().string());//convert reponse to string
             } catch (IOException e) {
                e.printStackTrace();
             }
      }
      @Override
      public void onFailure(Call<ResponseBody> call, Throwable t) {
          Log.i("error", t.getMessage());
      }
  });
如何修复它

更新

我的接口Api:

public interface SignUpApi {

@Multipart
@POST("signup_profile")
Call<SignUpResponseModel> uploadFile(@Part MultipartBody.Part file,
                                     @Part("sample_disc") RequestBody imageFile,
                                     @Part("name") RequestBody name,
                                     @Part("phone") RequestBody phone,
                                     @Part("email") RequestBody email,
                                     @Part("password") RequestBody password,
                                     @Part("username") RequestBody username);

}
邮差回应:


您将获得状态和消息响应,显示url中设置的变量可能存在冲突。我的意思是

@Part("sample_disc") RequestBody imageFile,
                                     @Part("name") RequestBody name,// May be these variable name are not exact 
                                     @Part("phone") RequestBody phone,
                                     @Part("email") RequestBody email,
                                     @Part("password") RequestBody password,
                                     @Part("username") RequestBody username);
{ “地位”:1, " 信息“:“成功” } 应为BEGIN_对象,但在第2行第1列路径处为字符串$

显然,在您的响应中,消息的值是字符串类型,但预期是JSON对象。
因此,它可能不是一个“成功”的JSON对象。

在您的响应中,您得到的响应是一个字符串,但它需要JSON对象 比如说

"{
 "status": 1,
 "message": "Successful"
 }" -- you are getting this 

 but you required -  {
 "status": 1,
 "message": "Successful"
 }

我认为当您从设备调用API时,您得到了错误的
JSON响应。这可能是服务器中的一个问题。假设当您使用postman调用您的API时,您将得到如下所示的预期响应(因为您的API调用是正常的)

当您从应用程序调用API时,您从服务器得到错误的
JSON响应
,这意味着API调用不正常[这可能是由于多种原因造成的]

"{
"status": 0,
"message": ""
}"
因此,您将得到一个异常,您期望响应中有一个对象,但得到的是一个字符串

您可以从API获取响应,并使用
okhttp
中的
ResponseBody
将其转换为字符串,如下所示:

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

    return new Retrofit.Builder()
            .baseUrl(AppConfig.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
{
"status": 1,
"message": "Successful"
}
Call<ResponseBody> call = getResponse.uploadFile(fileToUpload, filename, name, phone, email, password, username);
call.enqueue(new Callback<ResponseBody>() {
     @Override
     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
             try {
                // here you can what response you are getting.
                Log.d("JSON From Server", response.body().string());//convert reponse to string
             } catch (IOException e) {
                e.printStackTrace();
             }
      }
      @Override
      public void onFailure(Call<ResponseBody> call, Throwable t) {
          Log.i("error", t.getMessage());
      }
  });

发布您的API结束点。同时共享您的发布请求。发布更新,先生。对不起,我添加了它@Jakirhossain当我看到onResponse未工作时,添加onFailure。我在Throwable中看到这个错误。这些变量用于向服务器发送信息。这是错误的?这就是为什么说检查这些变量这些类型或错误发生在您的变量与服务器变量不匹配时如果提交的变量与服务器变量不匹配,没有问题。在web服务中,返回如下响应:echo json_encode(数组('status'=>1,'message'=>“Successful”);它不是JSON?在web服务中,返回如下响应:echo JSON_encode(数组('status'=>1,'message'=>“Successful”);它不是JSON?什么是“ResponseBooky”?我必须让“uploadFile”返回“okhttp3.ResponseBooky”?ResponseBooky是okhttp3类。