Android编程:使用VOLLEY向服务器发送数据
凌空图书馆的新手。
我有以下EditText,用户输入数据并按下注册按钮。我想将数据发送到hostinger托管的远程服务器。
EditText firstname = (EditText)findViewById(R.id.firstname);
EditText lastname = (EditText)findViewById(R.id.lastname);
EditText email = (EditText)findViewById(R.id.email);
EditText phone = (EditText)findViewById(R.id.phone);
EditText password = (EditText)findViewById(R.id.password);
EditText repeatpass = (EditText)findViewById(R.id.rptpassword);
Button regbtn = (Button)findViewById(R.id.regbtn);
Button tologin = (Button)findViewById(R.id.loginbtn);
如何将数据发布到服务器?
请包含服务器端的代码。
答案 0 :(得分:1)
一个简单的字符串请求:
RequestQueue queue = MyVolley.getRequestQueue();
StringRequest myReq = new StringRequest(Method.GET,
"http://www.google.com/",
createMyReqSuccessListener(),
createMyReqErrorListener());
queue.add(myReq);
}
private Response.Listener<String> createMyReqSuccessListener() {
return new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//TODO
}
};
}
private Response.ErrorListener createMyReqErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//TODO
}
};
}
在MyVolley课程中:
public static RequestQueue getRequestQueue() {
if (mRequestQueue != null) {
return mRequestQueue;
} else {
throw new IllegalStateException("RequestQueue not initialized");
}
}
如果你想添加String params很简单:
String uri = "www.google.com" + params;
RequestQueue queue = MyVolley.getRequestQueue();
StringRequest myReq = new StringRequest(Method.GET,
uri,
createMyReqSuccessListener(),
createMyReqErrorListener());
queue.add(myReq);
}