2016-04-05 9 views
0

私はretrofit 2でPOSTリクエストを実行する際に問題が発生しています。私は、RetrofitのGsonConverterFactoryを使用してPOSTを実行して、POJOオブジェクトをJSONに変換しようとしています。Retrofit 2.0.1投稿JSON - MalformedJsonException

GsonConverterFactoryを使用してボディをPOJOクラスに変換するGETリクエストがありますが、POSTでは動作しないようです(POJOからJSONへ)。私はAndroidスタジオでこれを使用しており、Gradleを使用してプロジェクトをビルドしています。ここで

compile 'com.squareup.retrofit2:retrofit:2.0.1' 
compile 'com.squareup.okhttp:okhttp:2.7.5' 
compile 'com.google.code.gson:gson:2.6.2' 
compile 'com.squareup.retrofit2:converter-gson:2.0.1' 

は、APIインタフェースのための私のコードです:

public interface EhabrApi { 
    @POST("/create-account") 
    Call<ResponseBody> createUser(@Body User user); 
} 

そして、これが要求させるためのコードです:

ここ
User newUser = new User("Bob", "Smith", "[email protected]", "password", "Seattle", "1990-01-01"); 

    System.out.println("User is " + new Gson().toJson(newUser)); 

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

    EhabrApi ehabrApi = retrofit.create(EhabrApi.class); 
    Call<ResponseBody> call = ehabrApi.createUser(newUser); 
    System.out.println("Request is " + new Gson().toJson(call.request())); 

    call.enqueue(new Callback<ResponseBody>() { 
      @Override 
      public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
       int statusCode = response.code(); 
       System.out.println("Recieved " + statusCode); 
      } 

      @Override 
      public void onFailure(Call<ResponseBody> call, Throwable t) { 
       System.out.println("Failed call " + t); 
       System.out.println("stack trace: "); 
       t.printStackTrace(); 
      } 
     } 
    ); 

が私の出力であるが、これらは私のGradleの依存関係していますスタックトレース:

Creating new account 
User is {"Birthday":"1990-01-01","City":"Seattle","Email":"[email protected]","FirstName":"Bob","LastName":"Smith","Password":"password"} 
Request is {"headers":{"namesAndValues":[]},"method":"POST","url":{"host":"mywebpage.com","password":"","pathSegments":["create-account"],"port":8080,"scheme":"http","url":"http://mywebpage.com","username":""}} 
Failed call com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ 
stack trace: 
com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ 
    at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1567) 
    at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1416) 
    at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:597) 
    at com.google.gson.stream.JsonReader.peek(JsonReader.java:429) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:201) 
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37) 
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25) 
    at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:116) 
    at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211) 
    at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106) 
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133) 
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
    at java.lang.Thread.run(Thread.java:818) 

EDIT: 今私は示唆されているようにインターセプターを入れました。 POSTリクエストは、右の私には見えます...ここでは、次のとおりです。

--> POST http://[mywebpageurl.com]/create-account http/1.1 
Content-Type: application/json; charset=UTF-8 
Content-Length: 125 
{"Birthday":"1990-01-01","City":"Seattle","Email":"[email protected]","FirstName":"Bob","LastName":"Smith","Password":"password"} 
--> END POST (125-byte body) 
<-- 200 OK http://[mywebpageurl.com]/create-account (100ms) 
+0

サービスから返されているJSONが、あなたのリクエストではなく、不正な形式であるようです。あなたはあなたの呼び出しが何を返すかを見ることができますか? –

+0

ええ、あなたが正しいと思います。しかし、コールバックでonResponseメソッドではなくonFailureメソッドを呼び出すので、応答を取得するメソッドを持たないCallオブジェクトにのみアクセスできます。リクエストだけです。 –

+0

私は[OkHttp LoggingInterceptor](https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor)を追加します。 'LogLevel'を' LogLevel.BODY'に設定すると、あなたのコンバーターに到達する前にあなたの応答が見えるはずです。 –

答えて

0

そう、それはあなたがJSONObjectを待つ意味が、あなたの結果として

Failed call com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

あなたのlogcatメッセージで非常に明確です不正な形式のJSONObjectを取得しようとしています。問題を解決するには、JSONの応答を確認してください。