Skip to content

Instantly share code, notes, and snippets.

@farooqkhan003
Created February 11, 2017 09:25
Show Gist options
  • Save farooqkhan003/265f724ba4d97e1c61fdef92350e2fae to your computer and use it in GitHub Desktop.
Save farooqkhan003/265f724ba4d97e1c61fdef92350e2fae to your computer and use it in GitHub Desktop.
package com.androidtechpoint.demo.androidvolley;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
TextView _name, _email, _response;
android.support.v7.widget.AppCompatButton _sendRequest;
ProgressBar _proProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Hooking the UI views for usage
_name = (TextView) findViewById(R.id.name);
_email = (TextView) findViewById(R.id.email);
_response = (TextView) findViewById(R.id.response);
_proProgressBar = (ProgressBar) findViewById(R.id.progressBar);
_sendRequest = (AppCompatButton) findViewById(R.id.send_request);
//hooking onclick listener of button
_sendRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
//this is the url where you want to send the request
//TODO: replace with your own url to send request, as I am using my own localhost for this tutorial
String url = "http://192.168.1.7/AndroidVolley/androidVolley.php";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the response string.
_response.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
_response.setText("That didn't work!");
}
}) {
//adding parameters to the request
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("name", _name.getText().toString());
params.put("email", _email.getText().toString());
return params;
}
};
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment