我正在Android Studio中编写一个执行GET请求的简单应用程序。 请求被发送到位于MongoDB实例之上的Eve rest接口。我是Android工作室开发的新手。
当Eve显示GET200代码时,HTTP请求成功。但是,我无法看到响应,我在模拟器上显示它时遇到问题。 每次按下按钮都会发送请求。
我的界面:
public interface inter {
@GET("/people/{name}")
Call<ServerResponse> Responses(@Path("name") String name);
public static final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://xxx.xxx.xxx.xx:5000")
.addConverterFactory(GsonConverterFactory.create())
.build();}
服务器响应类: _id,_updated_created和_etag是我的数据库中条目的部分。也许我应该包括所有参数,例如file和ISODate?
public ServerResponse(String _id, String _updated, String _created, String _etag)
{
this._id = _id;
this._updated = _updated;
this._created = _created;
this._etag = _etag;
}
}
网络呼叫类,位于MainActivity中:
public class NetworkCall extends AsyncTask<Call,Void,String> {
@Override
protected String doInBackground(Call... params) {
try {
Call<ServerResponse> call = params[0];
Response<ServerResponse> response = call.execute();
return response.body().toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result){
final TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(result);
}
}
在我的MainActivity中:
@Override
public void onClick(View view){
Snackbar.make(view,"Work in progress",Snackbar.LENGTH_LONG)
.setAction("Action",null).show();
inter Inter = inter.retrofit.create(inter.class);
final Call<ServerResponse> call;
call = Inter.Responses("aidan");
new NetworkCall().execute(call);
}
代码正确执行,我可以多次发送请求,但模拟器上没有显示任何内容。