我是java和Android的新手,我试图将一些变量传递到一个Retrofit GET-Call中。但是什么都不适合我,所以请你看看我的代码?
我需要改变什么来发送我的变量。
到服务器?
我的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接口类中添加查询参数。添加两个查询参数后,你的类应该是这样的。
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);
}
而在你的活动中,函数调用应该是这样的:
Call<List<Entries>> call = jsonPlaceHolderApi.getEntries(myActuelFullName,myOtherInfo);