使用Retrofit进行Google搜索

时间:2015-02-27 18:13:06

标签: json google-search retrofit

我正在尝试学习Retrofit,并希望将其用于Google搜索。我写了以下代码:

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);
    }
}

当我运行此代码时,我收到以下错误:

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

我是否必须使用Google搜索API(costum搜索)或Google搜索返回som其他类型的结果?

提前致谢。

0 个答案:

没有答案