Sergey Maslov
Sergey Maslov

Reputation: 758

Retrofit 2 response body contents only "<html>" string

I'm trying to understand how to use retrofit library, so I created an android project and a simple php script. My index.php file location is xampp's htdocs directory.

Here is my php script, here I just want to return a string when the script is executed

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php  
    echo "Success";
 ?> 
 </body>
</html>

On the android part I want to send a GET request

@GET("/")
Call<String> test();

Here I send the response and do necessary preparations

Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
                .setLenient()
                .create();        
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.43.169:80/")
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        IDataSyncApi dataSyncApi = retrofit.create(IDataSyncApi.class);       
        Callback<String> callback = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                String responseStr = gson.fromJson(response.body().toString(), String.class);
                Log.d("RESPONSE", responseStr);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        };
        Call<String> response = dataSyncApi.test();
        response.enqueue(callback);

The problem is that Response's body is always equals to "< html >". What am I doing wrong?

UPDATE 1

I tried using Wikipedia public API instead of mine, and result was the same. I got ""

 @GET("/w/api.php")
 Call<String> test(@QueryMap Map<String, String> argsMap);

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://en.wikipedia.org/")
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

Map<String, String> args = new HashMap<String, String>();
        args.put("action", "query");        
        args.put("meta", "siteinfo");
        args.put("siprop", "namespaces");
        Call<String> response = dataSyncApi.test(args);
        response.enqueue(callback);

Upvotes: 1

Views: 5554

Answers (4)

Enli
Enli

Reputation: 209

You can use ResponseBody Type. like below:

@Multipart
@POST("/android_upload_file/uploadfile.php")
Call<ResponseBody> upload(@Part("fileName") String des, @Part("file\"; filename=\"1.txt\"")RequestBody file);

Upvotes: 1

Sergey Maslov
Sergey Maslov

Reputation: 758

I figured it out by myself. The problem was that I used String as type for Call class (Call<String>). Instead, its better to use Call<ResponseBody>, so it becomes like this:

Service

 @GET("/w/api.php")
 Call<ResponseBody> test(@QueryMap Map<String, String> argsMap);

Making a request

Call<ResponseBody> response = dataSyncApi.test(args);
        response.enqueue(callback);

Upvotes: 2

trav3ll3r
trav3ll3r

Reputation: 30

Are you sure you're requesting the right PHP document? It could be that XAMPP cached the file or serves another file like index.html?

Alternatively, I'd change the PHP file to respond with JSON and add a requestHeader to your Retrofit call to accept JSON (or HTML, depending on what you're expecting)

Upvotes: 0

jbarat
jbarat

Reputation: 2460

You already have the response as a string in the Response<String> response. You try to deserialize it with gson which is for jsons not html.

If is's still not working try to log the response using a HttpLoggingInterceptor.

Upvotes: 0

Related Questions