Expected BEGIN_OBJECT but was String at line 5 column 1 path$. I am getting this error and turns out any of the solutions doesn't work

huangapple 未分类评论51阅读模式
英文:

Expected BEGIN_OBJECT but was String at line 5 column 1 path$. I am getting this error and turns out any of the solutions doesn't work

问题

我在Github的问题中发现,清理项目工作得很好,但那并没有帮助。我正在使用retrofit 2。以下是依赖项 -

implementation 'com.squareup.retrofit2:retrofit:2.8.1'
implementation 'com.squareup.retrofit2:converter-gson:2.8.1'
implementation 'com.google.code.gson:gson:2.8.5'

我的模型类

public class Register {
    private String email, password, firstname, lastname, snuid;

    public Register(String email, String password, String firstname, String lastname, String snuid) {
        this.email = email;
        this.password = password;
        this.firstname = firstname;
        this.lastname = lastname;
        this.snuid = snuid;
    }
}

我的JsonApiHolder类

public interface Pythonforeverybody {
    @GET("medicines")
    Call<List<Post>> getprods();

    @GET("medicines/{id}")
    Call<List<Post>> getprods(@Path("id") int id1);

    @POST("register/")
    Call<Register> getreg(@Body Register register);
}

以及MainActivity中的一个方法

private void postcontent() {
    Register register = new Register("ss501@snu.edu.in",
            "123456","Shardul","Singh","ss501");

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

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://Some API") 
            //我在这里提供了正确的API链接,没有问题
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    Pythonforeverybody jsonPlaceHolderApi = retrofit.create(Pythonforeverybody.class);
    Call<Register> call = jsonPlaceHolderApi.getreg(register);

    call.enqueue(new Callback<Register>() {
        @Override
        public void onResponse(Call<Register> call, Response<Register> response) {
            if (!response.isSuccessful()) {
                tv.setText("Code hh: " + response.code());
                return;
            }
            String content = "";
            content += "Code  " + response.code() + "\n";
            tv.append(content);
        }

        @Override
        public void onFailure(Call<Register> call, Throwable t) {
            tv.setText(t.getMessage());
        }
    });
}
英文:

I found on Github issues that cleaning the project works just fine but that didn't help.
I am using retofit 2. And these are the dependencies-

implementation 'com.squareup.retrofit2:retrofit:2.8.1'

implementation 'com.squareup.retrofit2:converter-gson:2.8.1'

implementation 'com.google.code.gson:gson:2.8.5'

My model class

public class Register {
    private String email,password,firstname,lastname,snuid;

    public Register(String email, String password, String firstname, String lastname, String snuid) {
        this.email = email;
        this.password = password;
        this.firstname = firstname;
        this.lastname = lastname;
        this.snuid = snuid;
    }
}

My JsonApiHolder class

public interface Pythonforeverybody {
    @GET(&quot;medicines&quot;)
    Call&lt;List&lt;Post&gt;&gt; getprods();

    @GET(&quot;medicines/{id}&quot;)
    Call&lt;List&lt;Post&gt;&gt; getprods (@Path(&quot;id&quot;) int id1);


    @POST(&quot;register/&quot;)
    Call &lt;Register&gt; getreg(@Body Register register);


}

And a method in Main activity

   private void postcontent() {
        Register register = new Register(&quot;ss501@snu.edu.in&quot;,
                &quot;123456&quot;,&quot;Shardul&quot;,&quot;Singh&quot;,&quot;ss501&quot;);

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

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(&quot;https:// Some API&quot;) 
                 //I gave the correct Api link over here, no problem with that 
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();


        Pythonforeverybody jsonPlaceHolderApi = retrofit.create(Pythonforeverybody.class);
        Call&lt;Register&gt; call = jsonPlaceHolderApi.getreg(register);



        call.enqueue(new Callback&lt;Register&gt;() {
            @Override
            public void onResponse(Call&lt;Register&gt; call, Response&lt;Register&gt; response) {
                if (!response.isSuccessful()) {
                    tv.setText(&quot;Code hh: &quot; + response.code());
                    return;
                }
                String content = &quot;&quot;;
                    content += &quot;Code  &quot; + response.code() + &quot;\n&quot;;
                   tv.append(content);
                }

            @Override
            public void onFailure(Call&lt;Register&gt; call, Throwable t)
            {
                tv.setText(t.getMessage());
            }


        });



    }
}

答案1

得分: 0

使用以下代码:

@POST("register/?format=json")
Call<Register> getreg(@Body Register register);

而不是:

@POST("register/")
Call<Register> getreg(@Body Register register);
英文:

Use this:

@POST(&quot;register/?format=json&quot;)
    Call &lt;Register&gt; getreg(@Body Register register);

instead of

@POST(&quot;register/&quot;)
    Call &lt;Register&gt; getreg(@Body Register register);

huangapple
  • 本文由 发表于 2020年5月3日 15:37:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/61571083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定