我正在学习使用Java进行改造2.1 我异步发送一个post请求,但即使成功执行onresponse方法,程序也不会终止1到1.5分钟。 Testjson.java是执行程序文件,它引用了两个类 - RegisteredUser.java 和 ApiCLient.java 。 Apiclient.java是一个包含post表单请求的接口,而RegisteredUser.java(未包含)是通过jsonschema2pojo创建的,它包含两个字段status和message以及getter setter函数。
public class TestJson {
public static void main(String[] args) {
// TODO Auto-generated method stub
String phone = "9717014775";
String email = "sanjay.kumar75033@gmail.com";
final String BASE_URL = "http://myapponline.in/API/";
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
ApiClient client = retrofit.create(ApiClient.class);
Call<RegisteredUser> userCall = client.getUser(phone, email,"users");
userCall.enqueue(new Callback<RegisteredUser>() {
@Override
public void onResponse(Call<RegisteredUser> arg0,
Response<RegisteredUser> arg1) {
// TODO Auto-generated method stub
RegisteredUser user = arg1.body();
if(user.getStatus().equals("1"))
System.out.println("alredy Registered");
else System.out.println("not registered");
}
@Override
public void onFailure(Call<RegisteredUser> arg0, Throwable arg1) {
// TODO Auto-generated method stub
System.out.println(arg1);
}
});
}
}
APIClient.java
public interface ApiClient {
@FormUrlEncoded
@POST("checkuserexists.php")
Call<RegisteredUser> getUser(@Field("phone") String phone,@Field("email") String email,@Field("usertype") String usertype);
}