【发布时间】:2017-04-11 23:13:47
【问题描述】:
我正在研究如何使用 Retrofit 来停止使用AsyncTask。我有一个名为Network 的接口发出我的GET 请求,一个ServiceGenerator 类创建我的Retrofit.Builder 和我的HTTP 拦截器,最后是我的MainActivity(我还实现了名为Results 的对象类)。
问题是,在我MainActivity 中的onResponse 方法(.enqueue)中:我不知道如何提取JSON 并在视图中放置“名称”标签。
这是我的ServiceGenerator 课程:
public class ServiceGenerator {
//URL base do endpoint. Deve sempre terminar com /
public static final String API_BASE_URL = "https://randomuser.me/";
public static <S> S createService(Class<S> serviceClass) {
//Instancia do interceptador das requisições
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.readTimeout(5, TimeUnit.SECONDS);
httpClient.addInterceptor(loggingInterceptor);
//httpClient.addInterceptor(loggingInterceptor).build();
//Instância do retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.client(httpClient.build())
.build();
return retrofit.create(serviceClass);
}
}
这是我的界面Network:
public interface Network {
@GET("api/")
Call<Results> randomUser();
}
最后是主要活动:
public class MainActivity2 extends AppCompatActivity {
private TextView nome;
private TextView sobrenome;
private TextView email;
private TextView endereco;
private TextView cidade;
private TextView estado;
private TextView username;
private TextView senha;
private TextView nascimento;
private TextView telefone;
private ImageView foto;
private ProgressDialog load;
public String gender;
public Results results;
public static final String BASE_URL = "https://randomuser.me/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nome = (TextView) findViewById(R.id.nome);
sobrenome = (TextView) findViewById(R.id.sobrenome);
email = (TextView) findViewById(R.id.email);
endereco = (TextView) findViewById(R.id.endereco);
cidade = (TextView) findViewById(R.id.cidade);
estado = (TextView) findViewById(R.id.estado);
username = (TextView) findViewById(R.id.username);
senha = (TextView) findViewById(R.id.senha);
nascimento = (TextView) findViewById(R.id.nascimento);
telefone = (TextView) findViewById(R.id.telefone);
foto = (ImageView) findViewById(R.id.foto);
Network network = ServiceGenerator.createService(Network.class);
Call<Results> requestUser = network.randomUser();
requestUser.enqueue(new Callback<Results>() {
@Override
public void onResponse(Call<Results> call, Response<Results> response) {
if (response.isSuccessful()) {
ProgressDialog progress = new ProgressDialog(MainActivity2.this);
progress.setTitle("sending...");
progress.show();
Results r = response.body();
/*MAYBE I SHOULD PUT SOMETHING HERE:*/
if (r != null) {
System.out.println();
// r.setGender(results.getGender());
nome.setText((CharSequence) results.setName(r.getName()));
//System.out.println("USER 2: " + response.raw() );
//r.setGender(r.getGender());
//Results r = new Results();
progress.dismiss();
// nome.setText(results.gender.toString());
// System.out.println("random user: CARAIO " + user.random);
//nome.setText((CharSequence) r.gender);
} else Toast.makeText(MainActivity2.this,"ERROR IN GET JSON",Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<Results> call, Throwable t) {
}
});
}
}
我的Results 类也是:
public class Results {
public static String gender;
public Results(){};
public static String getGender() {
return gender;
}
public static void setGender(String gender) {
Results.gender = gender;
}
public List<Name> getName() {
return name;
}
public Object setName(List<Name> name) {
this.name = name;
return null;
}
public List<Location> getLocation() {
return location;
}
public void setLocation(List<Location> location) {
this.location = location;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Login> getLogin() {
return login;
}
public void setLogin(List<Login> login) {
this.login = login;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public List<Picture> getPicture() {
return picture;
}
public void setPicture(List<Picture> picture) {
this.picture = picture;
}
public List<Name> name;
public List<Location> location;
public String email;
public List<Login> login;
public String phone;
public List<Picture> picture;
}
如果能帮到你,不胜感激,谢谢。
【问题讨论】:
标签: android json gson retrofit2