简体   繁体   English

从Android调用Rest API的更好方法

[英]Better approach to call rest api from android

I am new to android programming. 我是android编程的新手。 I am using rest call from android to query result and based on the queried result I allow user to navigate from one screen/activity to another. 我正在使用来自android的rest调用来查询结果,并且基于查询的结果,我允许用户从一个屏幕/活动导航到另一个屏幕/活动。 I have around 7 activity page and on each page I perform several operations for that I use rest call. 我大约有7个活动页面,并且在每个页面上我都会执行一些使用rest调用的操作。

The way I am invoking is using AsyncHttpClient 我调用的方式是使用AsyncHttpClient

Ex. 防爆。

    AsyncHttpClient client = new AsyncHttpClient();

    client.get("http://serverurl:8080/path1/path2/path3", params, new AsyncHttpResponseHandler() {

     //some code

    }

The one problem which I am facing is if I have to modify the url I need to modify in all the activity page. 我面临的一个问题是,如果我必须修改所有活动页面中需要修改的URL。

Is there a way from where I can modify once that can be used in every activity? 有没有一种方法可以修改一次并可以在每个活动中使用? Is there a better way? 有没有更好的办法? Please let me know. 请告诉我。

Use Retrofit 使用改造

public interface GitHubService {
  @GET("users/{user}/repos")
  Call<List<Repo>> listRepos(@Path("user") String user);

  @GET("users/repos/{id}")
  Call<Repo> getRepo(@Path("id") String id);

} }

Any kind of url changes can be done in this interface 可以在此界面中进行任何类型的url更改

Initialization of retrofit with base url 使用基本网址初始化改造

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com/")
    .build();

GitHubService service = retrofit.create(GitHubService.class);

Consuming Api 消费Api

Call<List<Repo>> repos = service.listRepos("octocat");
 repos.enqueue(new Callback<List<Repo>>() {
  @Override
  public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
   //Do something with response 
  }

  @Override
  public void onFailure(Call<List<String>> call, Throwable t) {
    //handle failure
  }
});

For more Retrofit 进行更多改造

There are several ways to do API calls in your android application. 有几种方法可以在您的android应用程序中进行API调用。

  • Using a library like Retrofit , you can perform API requests without any problems by using an interface. 使用Retrofit之类的库,您可以通过使用接口执行API请求,而不会出现任何问题。
  • You can also use the default Url connection that comes with Android to do all sorts of operations like GET and POST among others. 您还可以使用Android随附的默认网址连接来执行各种操作,例如GET和POST。

If you are worried about how to easily change the endpoint (url) , you can write your code so that you pass in a string param to your methods that way, you don't hard code the value. 如果您担心如何轻松更改端点(url) ,则可以编写代码,以便以这种方式将字符串参数传递给您的方法,而无需对值进行硬编码。

I am sure there are a lot more libraries out there and normally, it is a matter of choice and taste. 我敢肯定那里有很多图书馆,通常情况下,这取决于选择和品味。

I hope this helps you as you try to solve your problem! 希望这对您解决问题有帮助!

Just use a static variable. 只需使用静态变量即可。

public class YourClass {

    public static final String URL = "http://www.example.com/abc";

    public void performApiCall() {
        AsyncHttpClient client = new AsyncHttpClient();

        client.get(URL, params, new AsyncHttpResponseHandler() {
            //some code
        });
    }
}

You can then use the URL string from other classes: 然后,您可以使用其他类中的URL字符串:

public class SomeOtherClass {

    public void performSomeOtherApiCall() {
        AsyncHttpClient client = new AsyncHttpClient();

        client.get(YourClass.URL, params, new AsyncHttpResponseHandler() {
            //some other code
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM