我正在尝试使用 volley 服务执行 http 请求,我在这里找到了一个非常有用的答案,即如何为不同的 http 请求组织服务,因此您不需要为每个请求执行所有代码。
请求工作正常,我得到了我想要的结果,但它永远不会在 mainActivity 上输入 de 回调。
所以这永远不会被执行:
void initVolleyCallback(){
mResultCallback = new IResult() {
@Override
public void notifySuccess(String requestType,JSONObject response) {
Log.d("GJ","success");
}
@Override
public void notifyError(String requestType,VolleyError error) {
Log.d(TAG, "Volley requester " + requestType);
Log.d(TAG, "Volley JSON post" + "That didn't work!");
}
};
}
这是我的主要活动:
public class Register extends AppCompatActivity {
EditText usernameTxt;
EditText passwordTxt;
EditText emailTxt;
RequestQueue queue;
boolean formValid = false;
VolleyService mVolleyService;
IResult mResultCallback;
static final String TAG = "request12";
final String URL = "http://10.0.2.2:3000/register";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//hide status bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_register);
initVolleyCallback();
//inicialize queue with volley
queue = Volley.newRequestQueue(this);
//inicialize form fields
usernameTxt = (EditText)findViewById(R.id.username);
passwordTxt = (EditText)findViewById(R.id.password);
emailTxt = (EditText)findViewById(R.id.email);
//set text for developing stage
usernameTxt.setText("afcosta5");
passwordTxt.setText("moitarioavE10");
emailTxt.setText("filipecosta_10@hotmail.com");
}
public void register(View view) {
System.setProperty("http.keepAlive", "false");
//get form data
final String username = usernameTxt.getText().toString();
String password = passwordTxt.getText().toString();
String email = emailTxt.getText().toString();
Log.d("email",String.valueOf(isValidEmail(email)));
if (!isValidEmail(email)) {
emailTxt.setError("Invalid Email");
}
//inicialize a map with pair key value
final Map<String, String> params = new HashMap<String, String>();
// Add form fields to the map
params.put("username", username);
params.put("email", email);
params.put("password", password);
JSONObject sendObj = new JSONObject(params);
mVolleyService = new VolleyService(mResultCallback,this);
mVolleyService.postDataVolley(URL,sendObj);
void initVolleyCallback(){
mResultCallback = new IResult() {
@Override
public void notifySuccess(String requestType,JSONObject response) {
Log.d("GJ","success");
}
@Override
public void notifyError(String requestType,VolleyError error) {
Log.d(TAG, "Volley requester " + requestType);
Log.d(TAG, "Volley JSON post" + "That didn't work!");
}
};
}
我真的不知道问题在哪里,需要一些帮助
从"onCreate(("中删除"initVolleyCallback(("方法。实现"IResult"接口,如
public class Register extends AppCompatActivity implements IResult
那么你必须实现 IResult 的覆盖方法
@Override
public void notifySuccess(String requestType,JSONObject response) {
Log.d("GJ","success");
}
@Override
public void notifyError(String requestType,VolleyError error) {
Log.d(TAG, "Volley requester " + requestType);
Log.d(TAG, "Volley JSON post" + "That didn't work!");
}