【问题标题】:Cannot Query with Retrofit无法通过改造查询
【发布时间】:2016-07-24 00:28:24
【问题描述】:

我正在使用 Retrofit,我想根据用户输入的搜索词查询 FlickR。我想确保正确设置它,因为我在 Retrofit/REST 方面的经验有限。任何关于我的代码的教程、建议和评论都将不胜感激。

我已将一些项目标记为“不确定”,我不知道应该输入的确切内容。在 REST 中,“q”是否总是用来表示查询?

最后,我希望图像结果显示在 GridView 中并可供用户选择:

public class MainActivity extends AppCompatActivity {

private EditText mSearchTerm;
private Button mRequestButton;
private String mQuery;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSearchTerm = (EditText) findViewById(R.id.ediText_search_term);
    mRequestButton = (Button) findViewById(R.id.request_button);
    mRequestButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mQuery = mSearchTerm.getText().toString();
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("https://api.flickr.com/services/rest/")
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();


            ApiInterface apiInterface = retrofit.create(ApiInterface.class);
            Call<List<Photo>> call = apiInterface.getPhotos(mQuery);
            call.enqueue(new Callback<List<Photo>>() {
                @Override
                public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {

                }

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

                }
            });

        }
    });



}

//Synchronous vs. Asynchronous
public interface ApiInterface {
    @GET("?&method=flickr.photos.search&tags=<mQuery>&api_key=1c448390199c03a6f2d436c40defd90e&format=json")  //
    Call<List<Photo>> getPhotos(@Query("q") String photoSearchTerm);
        }

    }

【问题讨论】:

    标签: java android retrofit flickr


    【解决方案1】:

    几乎。顺便说一句,您似乎正在使用 Retrofit 1.9; Retrofit 2 可用,您现在可能想要切换,因为最终我认为 1.9 将被弃用。

    RestAdapter restAdapter=new RestAdapter.Builder()
            .setEndpoint("http://api.flickr.com/services")
            .setLogLevel(RestAdapter.LogLevel.FULL)//log your request
            .build();
     }
    
    public interface ApiInterface {
    @GET("/rest")  //
    void getPhotos(@Query("method") String method, @Query("api_key") String key, @Query ("user_id") String user, @Query("format") String format, @Query("city") String city, Callback<FlickrResponse> callback);
         }
    

    FlickrResponse 是一个 Java 类,代表您期望的响应。您可以将一些示例 JSON 插入其中,然后根据需要调整结果:http://www.jsonschema2pojo.org/。可能还有其他一些不完全正确的细节,但这应该可以帮助您入门。如果您想避免每次都传递密钥和用户 ID 等,您可以研究请求拦截器。这样你就可以定义一个拦截器,将它们注入到请求中,你可以从接口中删除这些参数。

    以下是有关迁移到 Retrofit 2 的一些信息:https://inthecheesefactory.com/blog/retrofit-2.0/en

    【讨论】:

    • 感谢您的帮助。我将尝试使用它,但是我注意到您删除了我查询的照片搜索部分。我基于此文档:flickr.com/services/api/flickr.photos.search.html
    • 我建议你多了解一点http/REST。 “?”之后的所有内容是查询参数。 nasch 将 method=flickr.photos.search 转换为方法参数。因此,您需要调用 getPhotos("flickt.photos.search") 以获得相同的方法值。另一个是搜索中的下一个字段。我建议您使用 http 客户端对其进行测试以了解发生了什么,例如 postman(浏览器插件)或 curl。
    • 非常感谢,您对 http/REST 教程有什么建议吗?我已经阅读了一些内容,但综合教程将是有益的。
    • 我将继续讨论这个问题,希望您知道一些文档/教程,以帮助我更好地了解如何使用 Retrofit 查询 FlickR。
    • 您是否希望了解 Flickr API 或 Retrofit,或两者兼而有之?您是坚持使用 Retrofit 1.9 还是迁移到 2?
    猜你喜欢
    • 2015-08-12
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多