你好我正在使用android中的volley开发一个应用程序。我希望使用POST方法向服务器发送一些参数但我不知道该怎么做。我在该活动类中使用了一个活动,我创建了一个将数据发送到服务器的功能。得到回应,
这是代码
package com.example.healthcoach;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class HealthCreateAccount extends Activity implements OnClickListener {
EditText et_fname, et_lname, et_email, et_password;
Button btn_create, btn_cancel;
String url = "http://192.168.1.32/get.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.create_user);
initViews();
}
public void initViews() {
btn_create = (Button) findViewById(R.id.signup_btn);
btn_cancel = (Button) findViewById(R.id.signup_btn_cancel);
et_fname = (EditText) findViewById(R.id.signnup_et_fname);
et_lname = (EditText) findViewById(R.id.signnup_et_lname);
et_email = (EditText) findViewById(R.id.signnup_et_email);
et_password = (EditText) findViewById(R.id.signnup_et_pass);
btn_cancel.setOnClickListener(this);
btn_create.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
switch (id) {
case R.id.signup_btn:
sendData();
Intent bsetup_intent=new Intent(HealthCreateAccount.this,HealthBasicSetUp.class);
startActivity(bsetup_intent);
break;
case R.id.signup_btn_cancel:
break;
default:
break;
}
}
public void sendData()
{
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest str=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String arg0) {
// TODO Auto-generated method stub
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
}
});
}
}
答案 0 :(得分:2)
您可以覆盖StringRequest
&#39} getParams()
方法。
<强> UDPATE 强>
public class HttpPostStringRequest extends StringRequest {
private Map<String, String> mParams = new HashMap<String, String>();
public HttpPostStringRequest(int method, String url,
Listener<String> listener, ErrorListener errorListener, Map<String, String> params) {
super(method, url, listener, errorListener);
mParams.putAll(params);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return mParams;
}
}
你可以将你的post params作为地图(比如键值对)给构造函数
答案 1 :(得分:0)
像这样覆盖getParams 它与Json和String请求一起工作
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", "Androidhive");
params.put("email", "abc@androidhive.info");
params.put("password", "password123");
return params;
}
};