如何在 android 中将 Retrofit 库用于 Get 和 Post 方法?
How to use Retrofit library for Get and Post methods in android?
我需要使用 Retrofit 库来解析来自 URL 的数据和 Post 通过传递一些数据到服务器的数据 Parameters.I 尝试了一些链接,但我没有理解清楚。谁能解释一下如何使用改造?并建议与最新版本相关的任何最佳改造链接。
提前致谢。
我正在开发一个原型应用程序,并在其中测试了 Retrofit。我将只提供相关代码和要求。
private void testRetrofit() throws IOException
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://jsonplaceholder.typicode.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
DemoEndpointInterface endpoint = retrofit.create(DemoEndpointInterface.class);
Call<DemoUser> call = endpoint.getUser("1");
DemoUser user = call.execute().body();
Log.d("$$$$ID : ", user.getId());
Log.d("$$$$User ID : ", user.getUserId());
Log.d("$$$$Title : ", user.getTitle());
Log.d("$$$$Body : ", user.getBody());
//Toast.makeText(this.getApplicationContext(), user.getTitle(), Toast.LENGTH_SHORT).show();
}
如下所述声明一个接口:
public interface DemoEndpointInterface {
@GET("/posts/{userId}")
Call<DemoUser> getUser(@Path("userId") String userid); }
DemoUser.java如下:
public class DemoUser {
String userId;
String id;
String title;
String body;
public DemoUser(String userId, String id, String title, String body) {
this.userId = userId;
this.id = id;
this.title = title;
this.body = body;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
在您的应用级别 gradle 文件中声明依赖项:
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
希望这对您有所帮助。如有任何问题,请告知。
谢谢。
在我解决之前,这个改造没有很好地支持新版本,所以改造 19 很酷。
我想你知道如何将 json 插入到 class model.So 中,我会告诉你如何传递 $DepartmentID=$_GET['DepartmentID']
首先初始化这个使用api,folder1/getDepartmentDetailsSingle.php?departmentID=departmentID
public interface RetrofitInternetApi {
@GET("/folder1/getDepartmentDetailsSingle.php")
void getDepartmentByID(@Query("departmentID") int departmentID, Callback<Response> callback);
}
那么函数就是
public void GetSINGLEDepartment(int departmentid){
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(
ROOT_URL).build();
//create an instance of the VehicleAPIService.
RetrofitInternetApi retrofitInternetApi = restAdapter
.create(RetrofitInternetApi.class);
retrofitInternetApi.getDepartmentByID(departmentid, new Callback<Response>() {
@Override
public void success(Response response, Response response2) {
BufferedReader reader = null;
//An string to store output from the server
String output = "";
try {
//Initializing buffered reader
reader = new BufferedReader(new InputStreamReader(response2.getBody().in()));
//Reading the output in the string
output = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("Success",output);
if(isJSONValid(output)==true){
//Saving to THE DATABASE
Log.d("FOUND JSON",output);
}else{
//data found is not Json
Log.d("NO JSON FOUND",output);
}
}
@Override
public void failure(RetrofitError error) {
Log.d("ERROR",error.getMessage());
}
});
稍后随着我的开发,我将更新如何在具有 json 的领域中使用它。
我需要使用 Retrofit 库来解析来自 URL 的数据和 Post 通过传递一些数据到服务器的数据 Parameters.I 尝试了一些链接,但我没有理解清楚。谁能解释一下如何使用改造?并建议与最新版本相关的任何最佳改造链接。
提前致谢。
我正在开发一个原型应用程序,并在其中测试了 Retrofit。我将只提供相关代码和要求。
private void testRetrofit() throws IOException
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://jsonplaceholder.typicode.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
DemoEndpointInterface endpoint = retrofit.create(DemoEndpointInterface.class);
Call<DemoUser> call = endpoint.getUser("1");
DemoUser user = call.execute().body();
Log.d("$$$$ID : ", user.getId());
Log.d("$$$$User ID : ", user.getUserId());
Log.d("$$$$Title : ", user.getTitle());
Log.d("$$$$Body : ", user.getBody());
//Toast.makeText(this.getApplicationContext(), user.getTitle(), Toast.LENGTH_SHORT).show();
}
如下所述声明一个接口:
public interface DemoEndpointInterface {
@GET("/posts/{userId}")
Call<DemoUser> getUser(@Path("userId") String userid); }
DemoUser.java如下:
public class DemoUser {
String userId;
String id;
String title;
String body;
public DemoUser(String userId, String id, String title, String body) {
this.userId = userId;
this.id = id;
this.title = title;
this.body = body;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
在您的应用级别 gradle 文件中声明依赖项:
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
希望这对您有所帮助。如有任何问题,请告知。
谢谢。
在我解决之前,这个改造没有很好地支持新版本,所以改造 19 很酷。 我想你知道如何将 json 插入到 class model.So 中,我会告诉你如何传递 $DepartmentID=$_GET['DepartmentID']
首先初始化这个使用api,folder1/getDepartmentDetailsSingle.php?departmentID=departmentID
public interface RetrofitInternetApi {
@GET("/folder1/getDepartmentDetailsSingle.php")
void getDepartmentByID(@Query("departmentID") int departmentID, Callback<Response> callback);
}
那么函数就是
public void GetSINGLEDepartment(int departmentid){
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(
ROOT_URL).build();
//create an instance of the VehicleAPIService.
RetrofitInternetApi retrofitInternetApi = restAdapter
.create(RetrofitInternetApi.class);
retrofitInternetApi.getDepartmentByID(departmentid, new Callback<Response>() {
@Override
public void success(Response response, Response response2) {
BufferedReader reader = null;
//An string to store output from the server
String output = "";
try {
//Initializing buffered reader
reader = new BufferedReader(new InputStreamReader(response2.getBody().in()));
//Reading the output in the string
output = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("Success",output);
if(isJSONValid(output)==true){
//Saving to THE DATABASE
Log.d("FOUND JSON",output);
}else{
//data found is not Json
Log.d("NO JSON FOUND",output);
}
}
@Override
public void failure(RetrofitError error) {
Log.d("ERROR",error.getMessage());
}
});
稍后随着我的开发,我将更新如何在具有 json 的领域中使用它。