Android volley gives BasicNetwork.performRequest: Unexpected response code 415 - android

While doing a POST request with android volley, My serverside consumes json type in request header, I am facing error 415. Though i have set the headers here by overriding getHeaders method, I am facing this unusual error while making the request.
can any body help me here?
I have also provided the source code below:
private void registerUser( final String email,
final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
// User successfully stored in MySQL
// Now store the user in sqlite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String email = user.getString("email");
String created_at = user
.getString("created_at");
// Inserting row in users table
db.addUser(email, uid, created_at);
Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
// Launch login activity
Intent intent = new Intent(
RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
#Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
//params.put("name", name);
params.put("email", email);
params.put("password", password);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

Related

There is no applicable constructor? Error appears on POST method in volley

I am making registration activity in android with volley but i get some error in post method please help
private void registerUser(final String name, final String email,
final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
// User successfully stored in MySQL
// Now store the user in sqlite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user
.getString("created_at");
// Inserting row in users table
db.addUser(name, email, uid, created_at);
Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
// Launch login activity
Intent intent = new Intent(
RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
#Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", email);
params.put("password", password);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
I am using volley:library-aar:1.0.0
And AppConfig.java
public class AppConfig {
public static String URL_REGISTER = "";
}
Error is
There is no applicable constructor to '(int.java.lang.string, com.andro.login.RegisterActivity.(anonymous), com.andro.login.RegisterActivity.(anonymous))'
Any solution for this prob?

app returning empty Toast

i am getting an empty Toast as response when i try to register a user to my sql database and no useful error hint is thrown in the logcat.
please what could possibly be the cause of this ?
registerProcess
private void registerProcess(final String name, final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
Functions.REGISTER_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response);
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
Functions logout = new Functions();
logout.logoutUser(getApplicationContext());
Bundle b = new Bundle();
b.putString("email", email);
Intent i = new Intent(RegisterActivity.this, EmailVerify.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtras(b);
startActivity(i);
pDialog.dismiss();
finish();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("message");
Toast.makeText(getApplicationContext(),errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
#Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", email);
params.put("password", password);
return params;
}
};
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}
web service
/**
* Adding new user to mysql database
* returns user details
*/
public function storeUser($fname, $lname, $email, $uname, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$result = mysql_query("INSERT INTO users(unique_id, firstname, lastname, email, username, encrypted_password, salt, created_at) VALUES('$uuid', '$fname', '$lname', '$email', '$uname', '$encrypted_password', '$salt', NOW())");
// check for successful store
if ($result) {
// get user details
$uid = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM users WHERE uid = $uid");
// return user details
return mysql_fetch_array($result);
} else {
return false;
}
}
There seems to be no error in the code, as far as I can see.
You should check the response you get with curl and see, what is returned.
to get the response from your web service you don't need JSON since you're doing a POST request by Volley. Your toast is empty because simply you're interpreting the response wrong and the objects like this one String errorMsg = jObj.getString("message"); will be empty.
To get the response you should do like this :
if (response.equalsIgnoreCase("yourResponseFromTheWebService")) {
...
Toast
} else if (response.equalsIgnoreCase("OtherPossibleResponse")){
....
Toast
} else{
....
Toast
}
EDIT :
I managed to spot two things in your web service code.
1- why are you trying to get informations from the server of the same user when you just sent them from your app. That doesn't make sense. You should attach your data as extra to your intent as you did with the email.
2- Volley expects a String type response and you giving him different formats at the same time.
So this a modified version of your code :
Web service :
public function storeUser($fname, $lname, $email, $uname, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$result = mysql_query("INSERT INTO users(unique_id, firstname, lastname, email, username, encrypted_password, salt, created_at) VALUES('$uuid', '$fname', '$lname', '$email', '$uname', '$encrypted_password', '$salt', NOW())");
// check for successful store
if ($result) {
echo "successful";
} else {
echo "notsuccessful";
}
}
Android
private void registerProcess(final String name, final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
Functions.REGISTER_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response);
hideDialog();
if (response.equalsIgnoreCase("successful")) {
Functions logout = new Functions();
logout.logoutUser(getApplicationContext());
Bundle b = new Bundle();
//add other informations you need like name ect..
b.putString("email", email);
Intent i = new Intent(RegisterActivity.this, EmailVerify.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtras(b);
startActivity(i);
pDialog.dismiss();
finish();
} else if(response.equalsIgnoreCase("notsuccessful")) {
Toast.makeText(getApplicationContext(),"new user wasn't registered successfully", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
#Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", email);
params.put("password", password);
return params;
}
};
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}
NOTE :
if you wanna know the error from your Mysql request don't show it on your app, just check it in your browser.
I hope this works for you

Android does not Toast after successful POST

I am sending data from an android client to a grails backend. I want two things to happen when data is successfully posted:
A toast with some of the params I have posted(In this case I want
only name)
An intent to open another activity.
However with my code, the parameters are posted successfully to the db but the toast and the intent do not happen.
Here is my code:
*/
private void registerUser(final String chname, final String chdesc, final String patientID) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
Configs.URL_LOGIN, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
// User successfully stored in MySQL
// Now store the user in sqlite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String chname = user.getString("name");
String chdesc = user.getString("description");
String patientID = user.getString("patient");
Toast.makeText(getApplicationContext(), "User successfully registered.", Toast.LENGTH_LONG).show();
// Launch login activity
Intent intent = new Intent(AddChronics.this,
ChronicsFragment.class);
startActivity(intent);
finish();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
#Override
protected Map<String, String> getParams() {
// Posting params to activity_register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", chname);
params.put("description", chdesc);
params.put("patient", patientID);
params.put("action", "chronicAdd");
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
Note: The API works just fine. I have checked and the params are successfully saved to the db.
Your code looks fine, the only thing that might prevent the intent and toast from happening is if your code gets cought by an excepetion here:
catch (JSONException e) {
e.printStackTrace();
}
Caused by one of these guys:
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
Did you check that?
Have you tried using activity context instead of applicationContext?

Android Volley error in POST request.

I have a problem with my Android code. I use volley lib to make http requests. In order to login into my app, I use the code below:
private void checkLogin(final String email_string, final String password_string) {
String tag_string_req = "req_login";
pDialog.setMessage("Logging in ...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.URL_LOGIN, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
session.setLogin(true);
/*
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user.getString("created_at");
db.addUser(name, email, uid, created_at);
*/
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
#Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=utf-8";
}
/*
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Accept-Charset:", "charset=utf-8");
headers.put("Content-type", "application/x-www-form-urlencoded");
return headers;
}
*/
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("email", email_string);
params.put("password", password_string);
return params;
}
};
int socketTimeout = 10000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, 1, 1);
strReq.setRetryPolicy(policy);
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
My API for logging in is the below piece of code:
$app->post('/login', function() use ($app) {
// check for required params
verifyRequiredParams(array('email', 'password'));
// reading post params
$email = $app->request()->post('email');
$password = $app->request()->post('password');
$response = array();
$db = new DbHandler();
// check for correct email and password
if ($db->checkLogin($email, $password)) {
// get the user by email
$user = $db->getUserByEmail($email);
if ($user != NULL) {
$response["error"] = false;
$response['name'] = $user['name'];
$response['email'] = $user['email'];
$response['apiKey'] = $user['api_key'];
$response['createdAt'] = $user['created_at'];
} else {
// unknown error occurred
$response['error'] = true;
$response['message'] = "An error occurred. Please try again";
}
} else {
// user credentials are wrong
$response['error'] = true;
$response['message'] = 'Login failed. Incorrect credentials';
}
echoRespnse(200, $response);
});
public function checkLogin($email, $password) {
// fetching user by email
$stmt = $this->conn->prepare("SELECT password_hash FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($password_hash);
$stmt->store_result();
if ($stmt->num_rows > 0) {
// Found user with the email
// Now verify the password
$stmt->fetch();
$stmt->close();
if (PassHash::check_password($password_hash, $password)) {
// User password is correct
return TRUE;
} else {
// user password is incorrect
return FALSE;
}
} else {
$stmt->close();
// user not existed with the email
return FALSE;
}
}
When I try and use postman to check my API, and POST the params in the URL, I get the correct response from my API. Therefore, I think there is no problem with it. However, when I try my Android code, it always returns me incorrect credentials. I am sure I've added the correct params. I think the problem is somewhere in my headers. Do you have any suggestions that could fix it?

Send post data to server using Volley android

I am trying to send some data to the server using the Volley library.
private void registerUser(final String email, final String username,
final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
// String status = jObj.getString("status");
// User successfully stored in MySQL
// Now store the user in sqlite
String name = jObj.getString("username");
String email = jObj.getString("email");
String password = jObj.getString("password");
// String created_at = user
//.getString("created_at");
// Inserting row in users table
// db.addUser(name, email);
// Launch login activity
Intent intent = new Intent(
RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("username", username);
params.put("password", password);
return params;
}
Unfortunately no json is sent and I get nothing back. Here is a sample of my logcat output. After sending a request successfully to the server, I want to get response with success/fail.
Register Response: ---- YOUR DATA ----
username=xxx&email=xxx%40gmail.com&password=xxxx&-------------------
05-05 14:56:55.002 2558-2558/app.victory.walking.thewalkingviktory
W/System.err﹕ org.json.JSONException: Value ---- of type java.lang.String
cannot be converted to JSONObject
05-05 14:56:55.002 2558-2558/app.victory.walking.thewalkingviktory
W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
05-05 14:56:55.002 2558-2558/app.victory.walking.thewalkingviktory
W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:160)
05-05 14:56:55.002 2558-2558/app.victory.walking.thewalkingviktory
W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:173)
Any help please? Thanx.
private void postUsingVolley() {
String tag_json_obj = "json_obj_req";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("posting...");
pDialog.show();
final String mVendorId = DeviceDetails.getInstance(mContext).getVendor_id();
String mUserId = UserModel.getInstance(mContext).getUser_id();
final HashMap<String, String> postParams = new HashMap<String, String>();
sendFeedbackParams.put("key1", value1);
sendFeedbackParams.put("key2", value2);
sendFeedbackParams.put("key3", value3);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
ApplicationData.POST_URL, new JSONObject(postParams),
new com.android.volley.Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//Log.d("TAG", response.toString());
try {
//Toast.makeText(mContext, response.getString("message"), Toast.LENGTH_LONG).show();
Toast.makeText(mContext, "Thank you for your post", Toast.LENGTH_LONG).show();
if (response.getBoolean("status")) {
pDialog.dismiss();
finish();
}
} catch (JSONException e) {
Log.e("TAG", e.toString());
}
pDialog.dismiss();
}
}, new com.android.volley.Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//VolleyLog.d("TAG", "Error: " + error.getMessage());
pDialog.dismiss();
if (isNetworkProblem(error)) {
Toast.makeText(mContext, "Internet Problem", Toast.LENGTH_SHORT).show();
}
}
}) {
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return getRequestHeaders();
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(8000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
Use Volley like this,... It is working for me.
First of all you are not sending json to your server. You are sending parametrized url with post method and getting json text as response.
I think the problem is the response from the server which is supposedly json. But from your log, this text "---- YOUR DATA ----" which you get from Log.d(TAG, "Register Response: " + response.toString()); is not at all json formatted . So you need to modify your server to respond in correct json format.
This is the solution. I had to use the JsonObjectRequest class and not the StringRequest on. What JsonRequest does is that it converts your HashMap key-value pairs into a JSON Format.
private void registerUser(String email_address,String username, String
password) {
String tag_json_obj = "json_obj_req";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("posting...");
pDialog.show();
final String mVendorId =
DeviceDetails.getInstance(mContext).getVendor_id();
String mUserId = UserModel.getInstance(mContext).getUser_id();
String location = getResources().getConfiguration().locale.getCountry();
final HashMap<String, String> postParams = new HashMap<String, String>
();
postParams.put("username", username);
postParams.put("email", email_address);
postParams.put("password", password);
postParams.put("location", location);
Response.Listener<JSONObject> listener;
Response.ErrorListener errorListener;
final JSONObject jsonObject = new JSONObject(postParams);
JsonObjectRequest jsonObjReq = new
JsonObjectRequest(AppConfig.URL_REGISTER, jsonObject,
new com.android.volley.Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//Log.d("TAG", response.toString());
try {
Toast.makeText(getApplicationContext(),
response.getString("message"), Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), "Thank
you for your post", Toast.LENGTH_LONG).show();
if (response.getString("status").equals("success")){
session.setLogin(true);
pDialog.dismiss();
Intent i = new
Intent(RegisterActivity.this,Welcome.class);
startActivity(i);
finish();
}
} catch (JSONException e) {
Log.e("TAG", e.toString());
}
pDialog.dismiss();
}
}, new com.android.volley.Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//VolleyLog.d("TAG", "Error: " + error.getMessage());
pDialog.dismiss();
}
}) {
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
JSon post
public void makePostUsingVolley()
{
session = new SessionManager(getActivity().getApplicationContext());
session.checkLogin();
HashMap<String, String> user = session.getUserDetails();
final String token = user.get(SessionManager.KEY_NAME);
//Toast.makeText(getActivity().getApplicationContext(),name, Toast.LENGTH_SHORT).show();
final Map<String, String> params = new HashMap<String, String>();
//params.put("Employees",name);
String tag_json_obj = "json_obj_req";
String url = "enter your url";
final ProgressDialog pDialog = new ProgressDialog(getApplicationContext());
pDialog.setMessage("Loading...");
pDialog.show();
StringRequest req = new StringRequest(Request.Method.GET,url,
new Response.Listener<String>() {
// final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
//"http://emservices.azurewebsites.net/Employee.asmx/CheckUserGet", new Response.Listener<JSONObject>() {
#Override
public void onResponse(String response) {
JSONObject json;
// Toast.makeText(getActivity().getApplicationContext(),"dfgghfhfgjhgjghjuhj", Toast.LENGTH_SHORT).show();
//Toast.makeText(getActivity().getApplicationContext(),obb.length(), Toast.LENGTH_SHORT).show();
// JSONObject data=obj.getJSONObject("Employee_Name");
ObjectOutput out = null;
try {
json = new JSONObject(response);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.hide();
// Toast.makeText(getApplicationContext(),"hi", Toast.LENGTH_SHORT).show();
Log.d("", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("", "Error: " + error.getMessage());
Toast.makeText(getActivity().getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
pDialog.hide();
// hide the progress dialog
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("username",name);
params.put("password",password);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req, tag_json_obj);
}
requestQueue= Volley.newRequestQueue(MainActivity.this);
StringRequest request=new StringRequest(Request.Method.PUT, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this, ""+response, Toast.LENGTH_SHORT).show();
Log.d("response",response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("name", name.getText().toString().trim());
jsonObject.put("email", email.getText().toString().trim());
jsonObject.put("phone", phone.getText().toString().trim());
} catch (JSONException e) {
e.printStackTrace();
}
Map<String, String> params = new HashMap<String, String>();
params.put("message", jsonObject.toString());
return params;
}
};
requestQueue.add(request);
[["deep","dee#gmail.com","8888999999"]]

Categories

Resources