I have a question for Retrofit1.9.

Asked 2 years ago, Updated 2 years ago, 26 views

Question 1

Upload Image File

https://futurestud.io/blog/retrofit-how-to-upload-files

You have followed this link.

As a result, the file was successfully uploaded to the server.

However, the log is dropping to failure.

I looked up the issue with Google, but I couldn't solve it ㅠ<

Interface modification part

    @Multipart
    @POST("/SellBook/uploadFile.php")
    void upload(@Part("myfile") TypedFile file,
                @Part("file_rename") String file_rename,
                Callback<String> cb);

Upload file

private void uploadFile(File file, String file_rename) {
    Retrofit_api service = ServiceGenerator.createService(Retrofit_api.class);
    TypedFile typedFile = new TypedFile("multipart/form-data", file);

    service.upload(typedFile,file_rename, new Callback<String>() {
        @Override
        public void success(String s, Response response) {
            Log.e("Upload", "success");
        }
        @Override
        public void failure(RetrofitError error) {
            Log.e("Upload", error.getMessage());
        }
    });
}

Error Log

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

android

2022-09-22 21:17

4 Answers

According to the error log you uploaded, it seems to be caused by Resnpose not meeting the JSON standard. It would be better to check the server response after uploading the file. When you create the RestAdapter.Builder, you can view all network logs by setting the log level to LogLevel.FULL as follows:

RestAdapter.Builder builder = new RestAdapter.Builder()  
    .setEndpoint(API_LOCATION)
    .setLogLevel(RestAdapter.LogLevel.FULL) 
    .setClient(new OkClient(new OkHttpClient()));

After uploading the file, try copying and pasting the Response log that comes down to the website below. You can verify that the format meets the JSON specification.


2022-09-22 21:17

The Response body does not exist in the log, but the Retrofit interface you created is defined as the Response body to receive BoardCreate, so it seems to be caused.

@POST("/SellBook/register_book.php")
void create_board(@Body BoardCreate boardCreate, Callback<BoardCreate> bd);

Please drop the Response body from the server or modify the client code as follows. (Change the parameters of the success function to Void type.)

@POST("/SellBook/register_book.php")
void create_board(@Body BoardCreate boardCreate, Callback<Void> response);


2022-09-22 21:17

Link: https://futurestud.io/blog/retrofit-send-objects-in-request-body I am sending the data in the body. If you look at the data that comes out after taking a log, it seems to meet the JSON standard, but the error seems to be a problem with the JSON standard.

@POST("/SellBook/register_book.php")
void create_board(@Body BoardCreate boardCreate, Callback<BoardCreate> bd);
Retrofit_api service = ServiceGenerator.createService(Retrofit_api.class);
BoardCreate boardCreate = new BoardCreate(user_id,data_group,data_price,data_board,data_phone,data_image1,data_image2,data_image3);

        service.create_board(boardCreate, new Callback<BoardCreate>() {

            @Override
            public void success(BoardCreate boardCreate, Response response) {
                Log.d("TAG","success");
            }

            @Override
            public void failure(RetrofitError error) {
                Log.e("TAG",error.getMessage());
            }
        });

Error content

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 2 path $

json value captured in log

{"book_category":"University of Social Sciences,"book_content":"ㄱㄴㄷㄱ,"","book_image1":","book_image2":"book_image3":":""book_phone":"12312341235""",""book_content":"""prㄱ"id52"""us"":"ice35"1170er":,"142""ookb"}:""p"""age"c"""_"b"사회"b"ook"b"b",""{":"

==>

{  
   "book_category": "University of Social Sciences",
   "book_content": "Theatrical",
   "book_image1":"21135270_1465574585737_Screenshot_2016-06-06-12-23-28.png",
   "book_image2":"",
   "book_image3":"",
   "book_phone":"12341234123",
   "book_price":"234",
   "user_id":"21135270"
}


2022-09-22 21:17

--> HTTP POST http://request address/register_book.php
D/Retrofit: Content-Type: application/json; charset=UTF-8
D/Retrofit: Content-Length: 184
D/Retrofit: {"book_category":"book_content":"test","book_image1":"book_image2":","book_image3":"book_phone":"00000000000","book_price":"5000""112":35000"2" ID2"
D/Retrofit: ---> END HTTP (184-byte body)
D/Retrofit: <--- HTTP 200 http://request address/register_book.php (412 ms)
D/Retrofit: Server: nginx
D/Retrofit: Date: Sat, 11 Jun 2016 01:49:55 GMT
D/Retrofit: Content-Type: text/html
D/Retrofit: Transfer-Encoding: chunked
D/Retrofit: Connection: keep-alive
D/Retrofit: Vary: Accept-Encoding
D/Retrofit: P3P: CP='NOI CURa ADMa DEVa TAIa OUR DELa BUS IND PHY ONL UNI COM NAV INT DEM PRE'
D/Retrofit: OkHttp-Sent-Millis: 1465609796321
D/Retrofit: OkHttp-Received-Millis: 1465609796670
D/Retrofit: "success"  
D/Retrofit: <--- END HTTP (14-byte body)
-----------------------------------------------------------------------------------------
E/TAG: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 2 path $

Currently, DB will run a query and drop the success, but the value is empty if the values are not crossed correctly.


2022-09-22 21:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.