我正在尝试使用排球服务进行http请求,我发现了一个非常有用的答案here,关于如何为不同的http请求组织服务,所以你不需要做所有这些每个请求的代码。
请求工作正常,我得到了我想要的结果,但它从未在mainActivity上输入de callback。
所以这永远不会被执行:
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!");
}
};
}
我真的不知道问题出在哪里,需要一些帮助
答案 0 :(得分:0)
从“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!");
}