I am new to android programming. 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. I have around 7 activity page and on each page I perform several operations for that I use rest call.
我是android编程的新手。我正在使用来自android的rest调用来查询结果,并根据查询结果我允许用户从一个屏幕/活动导航到另一个屏幕/活动。我有大约7个活动页面,在每个页面上我执行了几个操作,因为我使用了休息呼叫。
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.
一旦可以在每个活动中使用,我有什么办法可以修改吗?有没有更好的办法?请告诉我。
3 个解决方案
#1
5
Use Retrofit
使用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
用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
更多改造
#2
0
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附带的默认Url连接来执行各种操作,例如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!
我希望这可以帮助您解决问题!
#3
0
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
});
}
}
#1
5
Use Retrofit
使用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
用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
更多改造
#2
0
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附带的默认Url连接来执行各种操作,例如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!
我希望这可以帮助您解决问题!
#3
0
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
});
}
}