2016-07-24 74 views
1

我正在使用Retrofit,我想根据用户输入的搜索词来查询FlickR。我想确保我的设置正确,因为我对Retrofit/REST的经验有限。任何教程,建议和评论我的代码将不胜感激。无法通过更新查询

我已经标记了一些项目“UNSURE”,我不知道应该输入什么。用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); 
     } 

    } 

回答

2

差不多。 BTW看起来像你正在使用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/。可能有一些其他细节不完全正确,但应该让你开始。如果您希望避免每次传递密钥和用户标识等,可以研究请求拦截器。这样你就可以定义一个拦截器,将它们注入到请求中,并且你可以从接口中删除这些参数。

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

+0

感谢您的帮助。我会试验它,但是我注意到你删除了我的查询的照片搜索部分。我基于这个文档:https://www.flickr.com/services/api/flickr.photos.search.html – tccpg288

+1

我建议你学习一些关于HTTP/REST的更多信息。 “?”之后的所有内容是查询参数。 nasch将method = flickr.photos.search转换为方法参数。所以你需要调用getPhotos(“flickt.photos.search”)来获得相同的方法值。另一个是搜索中的下一个字段。我建议你使用http客户端来测试它,以了解发生了什么,比如邮递员(浏览器插件)或卷曲。 – Lxu

+0

非常感谢,您对http/REST教程有任何建议吗?我已阅读了一些内容,但综合教程将会有所帮助。 – tccpg288