3

I am trying to learn Retrofit and want to use it for Google search. I have written following code:

public class RetrofitSample {
    private static final String URL = "https://www.google.com";

    interface Google {
        @GET("/search?q=retrofit")
        String search();
    }

    public static void main(String... args) {
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(URL)
                .build();
        Google goog = restAdapter.create(Google.class);
        String result = goog.search();
        System.out.println(result);
    }
}

When I run this code I am getting following error:

Exception in thread "main" retrofit.RetrofitError: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 12 path $
    at retrofit.RetrofitError.conversionError(RetrofitError.java:33)
    at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:383)
    at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
    at $Proxy0.search(Unknown Source)
    at RetrofitSample.main(RetrofitSample.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 12 path $
    at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:67)
    at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:367)
    ... 8 more
Caused by: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 12 path $
    at com.google.gson.Gson.assertFullConsumption(Gson.java:786)
    at com.google.gson.Gson.fromJson(Gson.java:776)
    at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:63)
    ... 9 more
Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 12 path $
    at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1573)
    at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1423)
    at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:546)
    at com.google.gson.stream.JsonReader.peek(JsonReader.java:429)
    at com.google.gson.Gson.assertFullConsumption(Gson.java:782)
    ... 11 more

Do I have to use Google search API (costum search) or is Google search returning som other type as result?

Thanks in advance.

Mukaddes
  • 307
  • 2
  • 12

0 Answers0