如何在 Retrofit 中将变量传递给 GET 参数
How to pass Variables into GET-Parameters in Retrofit
我是 java 和 Android 的新人。
我正在尝试将一些变量传递到 Retrofit GET-Call 中。但对我来说没有任何用处,所以请你看看我的代码?
我必须更改什么才能发送我的变量:
- 我的ActuelFullName
- 我的其他信息
到服务器?
我的java-文件:
public class Orte extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orte);
Intent myIntent = getIntent(); // gets the previously created intent
myActuelFullName = myIntent.getStringExtra("paramFullName"); // will return "paramFullName"
getEntries(myActuelFullName);
}
private void getEntries(String myActuelFullName) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://myDomain.de/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
String myOtherInfo = "testing123";
Call<List<Entries>> call = jsonPlaceHolderApi.getEntries();
Log.d("DEBUG", myActuelFullName ); // at this point the Variable is known!
call.enqueue(new Callback<List<Entries>>() {
@Override
public void onResponse(Call<List<Entries>> call, Response<List<Entries>> response) {
if (!response.isSuccessful()) {
//textViewResult.setText("Code: " + response.code());
return;
}
//
@Override
public void onFailure(Call<List<Entries>> call, Throwable t) {
// textViewResult.setText(t.getMessage());
}
});
还有我的占位符-Api:
public interface JsonPlaceHolderApi {
@GET("get_entries.php")
Call<List<Entries>> getEntries();
@POST("http://myDomain.de/api/mypost.php/")
Call<Post> createPost(@Body Post post);
}
您必须先在API界面class中添加查询参数。添加两个查询参数后,您的 class 应如下所示:
public interface JsonPlaceHolderApi {
@GET("get_entries.php")
Call<List<Entries>> getEntries(@Query("myActuelFullName") String myActuelFullName, @Query("myOtherInfo") String myOtherInfo);
@POST("http://myDomain.de/api/mypost.php/")
Call<Post> createPost(@Body Post post);
}
在你的 activity 中,函数调用应该是这样的:
Call<List<Entries>> call = jsonPlaceHolderApi.getEntries(myActuelFullName,myOtherInfo);
我是 java 和 Android 的新人。 我正在尝试将一些变量传递到 Retrofit GET-Call 中。但对我来说没有任何用处,所以请你看看我的代码?
我必须更改什么才能发送我的变量:
- 我的ActuelFullName
- 我的其他信息
到服务器?
我的java-文件:
public class Orte extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orte);
Intent myIntent = getIntent(); // gets the previously created intent
myActuelFullName = myIntent.getStringExtra("paramFullName"); // will return "paramFullName"
getEntries(myActuelFullName);
}
private void getEntries(String myActuelFullName) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://myDomain.de/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
String myOtherInfo = "testing123";
Call<List<Entries>> call = jsonPlaceHolderApi.getEntries();
Log.d("DEBUG", myActuelFullName ); // at this point the Variable is known!
call.enqueue(new Callback<List<Entries>>() {
@Override
public void onResponse(Call<List<Entries>> call, Response<List<Entries>> response) {
if (!response.isSuccessful()) {
//textViewResult.setText("Code: " + response.code());
return;
}
//
@Override
public void onFailure(Call<List<Entries>> call, Throwable t) {
// textViewResult.setText(t.getMessage());
}
});
还有我的占位符-Api:
public interface JsonPlaceHolderApi {
@GET("get_entries.php")
Call<List<Entries>> getEntries();
@POST("http://myDomain.de/api/mypost.php/")
Call<Post> createPost(@Body Post post);
}
您必须先在API界面class中添加查询参数。添加两个查询参数后,您的 class 应如下所示:
public interface JsonPlaceHolderApi {
@GET("get_entries.php")
Call<List<Entries>> getEntries(@Query("myActuelFullName") String myActuelFullName, @Query("myOtherInfo") String myOtherInfo);
@POST("http://myDomain.de/api/mypost.php/")
Call<Post> createPost(@Body Post post);
}
在你的 activity 中,函数调用应该是这样的:
Call<List<Entries>> call = jsonPlaceHolderApi.getEntries(myActuelFullName,myOtherInfo);