Hello i am developing an app using volley in android.I want send some params to server using POST method But i don't know how to do it. I am using an activity in that activity class i have created a function for sending data to the server & getting the response,
Here is the code
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
}
});
}
}
asked Jan 29, 2015 at 5:41
TechChain
8,98832 gold badges119 silver badges246 bronze badges
2 Answers 2
You can override StringRequest's getParams() method.
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;
}
}
you can give your post params as a map(like key-value pairs) to the constructor
Sign up to request clarification or add additional context in comments.
3 Comments
TechChain
but i an not extending the request class?
jobcrazy
You need to extending StringRequest, cause default implementation of getParams() in Request is just return null.
TechChain
can you post some sample code for that like how i would be calling that class from my activity???
just override getParams like this
its work with Json and String request
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", "[email protected]");
params.put("password", "password123");
return params;
}
};
answered Jan 29, 2015 at 5:52
aiwiguna
2,9072 gold badges18 silver badges27 bronze badges
Comments
default