Volley give me only: onErrorResponse - android

I start to work with volley and when i try to make a post request it give me only errore.
Can you guys tell me when im wrong?
Im working on the Android side of the project with VolleyLibrary.
public class MainActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = findViewById(R.id.etUsername);
etPassword = findViewById(R.id.etPassword);
btnLogin = findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Login();
}
});
}
private void Login(){
String url = "http://myapies.youcantwatch.it/loginuser_module";
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if(!response.trim().equals("errors")){
Intent intent = new Intent(MainActivity.this, LoggedActivity.class);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "Login Fallito", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "La chiamata non รจ andata a buon fine", Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("firstName","adkdmadm");
params.put("password","asdasdjn");
params.put("secretKey","dlwdmkemd3d455");
params.put("username", etUsername.getText().toString().trim());
params.put("passwordasdad", etPassword.getText().toString().trim());
return super.getParams();
}
};
requestQueue.add(stringRequest);
}
}

Replace StringRequest->JSONObjectRequest and check.
Pass request parameters in JSONObject, not in the header,
only pass Content-Type->application/json in header.

change volley to another libs like retrofit volley long time not updated

Related

Send data to laravel with Android volley

I want to send the user-id data I received with android to laravel.
codes for android;
public class MainActivity extends AppCompatActivity {
public String userId;
Button send;
private static final String KEY_USERNAME ="userId" ;
String insertUrl ="http://127.0.0.1:8000/registerDevices";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userId= "12345";
send=(Button) findViewById(R.id.buttonPush);
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(getApplicationContext(), userId, Toast.LENGTH_LONG).show();
registerUser();
}
});
}
public void registerUser(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, insertUrl,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), userId, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_LONG).show();
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_USERNAME,userId);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
My route in laravel;
Route::post('/registerDevices', 'RegisterController#registerdevices');
public function registerdevices(Request $request)
{
\Log::info('api call from andriod');
$jsonPostedData = Input::get('userId');
return $this->service->registerdevices($jsonPostedData);
}
my code does not work and the error it gives;
java.net.ConnectException:Connection Refused
Can you help me?

Unable to POST data to server in android using WebServices API

I want to POST data to server but i get
Volley: [1726] BasicNetwork.performRequest: Unexpected response code 415 for http://192.158.20.43:8080/Api/employee/create" error.
public class RegisterTextActivity extends AppCompatActivity {
EditText emailBox, passwordBox,firstName,lastName;
Button registerButton;
TextView loginLink;
String URL = "http://192.158.20.43:8080/Api/employee/create";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_text);
firstName=(EditText)findViewById(R.id.firstname);
lastName=(EditText)findViewById(R.id.lastname);
emailBox = (EditText)findViewById(R.id.emailBox);
passwordBox = (EditText)findViewById(R.id.passwordBox);
registerButton = (Button)findViewById(R.id.registerButton);
loginLink = (TextView)findViewById(R.id.loginLink);
registerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>(){
#Override
public void onResponse(String s) {
if(s.equals("true")){
Toast.makeText(RegisterTextActivity.this, "Registration Successful", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(RegisterTextActivity.this, "Can't Register", Toast.LENGTH_LONG).show();
}
}
},new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError volleyError) {
volleyError.printStackTrace();
Toast.makeText(RegisterTextActivity.this, "Some error occurred -> "+volleyError, Toast.LENGTH_LONG).show();
}
}) {
#Override
public String getBodyContentType() {
return "application/json;charset=utf-8";
}
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("firstname",firstName.getText().toString().trim());
parameters.put("lastname",lastName.getText().toString().trim());
parameters.put("email", emailBox.getText().toString().trim());
parameters.put("phone", passwordBox.getText().toString().trim());
return parameters;
}
};
RequestQueue rQueue = Volley.newRequestQueue(RegisterTextActivity.this);
/
rQueue.add(request);
}
});
loginLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(RegisterTextActivity.this, LoginTestActivity.class));
}
});
}
}
API work fine in POSTMAN but not work properly here.
try changing your Content-Type
application/json;charset=utf-8
to
application/json

Android: checking database with volley library constantly

I started learning Android and Java a week ago and now I am trying to make an login application. I am using Volley libary to communicate with my server. I have done the login part. Now, what I want to do is to check the database every minute to see if the password or the username somehow changed. If the information in the database is changed, app will automaticly logout the user.
If you can explain which tools(Services,BroadcastReceivers) I can use and how can I achieve it, as I am not very experienced.
This is what I tried and failed:
loginChecker.class
public class loginChecker extends Service {
public loginChecker() {
}
public static String username;
public static String password;
private loginChecker mInstance = this;
public static boolean loginCheck= true;
public static String responseG = "failed";
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle b=intent.getExtras();
username = b.getString("username");
password = b.getString("password");
final String URL = ".........";
final RequestQueue requestQueue = Volley.newRequestQueue(mInstance);
new Thread(new Runnable(){
public void run() {
do{
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
responseG = response;
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
responseG = "error";
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("username", username);
hashMap.put("password", password);
return hashMap;
}
};
requestQueue.add(request);
switch(responseG){
case "successful" :
loginCheck = true;
break;
case "failed" :
loginCheck= false;
break;
case "error" :
loginCheck = false;
break;
}
}
while(loginCheck == true || responseG == "successful");
}
}).start();
Toast.makeText(getApplicationContext(), "LOOP ENDED", Toast.LENGTH_SHORT).show();
return START_REDELIVER_INTENT;
}
#Override
public void onDestroy() {
final Intent mainActivity = new Intent(mInstance, MainActivity.class);
mainActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mainActivity);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
MainActivity.class
public class MainActivity extends AppCompatActivity {
private RequestQueue requestQueue;
private static final String URL = "........";
private StringRequest request;
private TextView text;
private EditText userName, passWord;
private Button loginButton;
public MainActivity mInstance = this;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.textView);
userName = (EditText) findViewById(R.id.userName);
passWord = (EditText) findViewById(R.id.passWord);
loginButton = (Button) findViewById(R.id.loginButton);
requestQueue = Volley.newRequestQueue(this);
final Intent profilePage = new Intent(this, Profile.class);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick (View v){
loginButton.setEnabled(false);
request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
text.setText(response);
switch(response){
case "successful" :
Intent loginCheckerService = new Intent(mInstance, com.erenyenigul.apps.starter.services.loginChecker.class);
Bundle b = new Bundle();
b.putString("username", String.valueOf(userName.getText()));
b.putString("password", String.valueOf(passWord.getText()));
loginCheckerService.putExtras(b);
startService(loginCheckerService);
startActivity(profilePage);
finish();
break;
case "failed" :
Toast.makeText(getApplicationContext(), "Username or Password you entered is wrong!", Toast.LENGTH_LONG).show();
loginButton.setEnabled(true);
break;
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "There is a problem with our servers or you don't have internet connection!", Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("username", userName.getText().toString());
hashMap.put("password", passWord.getText().toString());
return hashMap;
}
};
requestQueue.add(request);
}
}
);
}
}
There is also a file called Profile.class but it is empty. I tried this but the loop lasted one tour. It stopped even though the connection was ok and the data wasn't changed.

HTTP Post Request using Volley

I code to send some message and email using HTTP post using Volley.
when i run the emulator using Genymotion.Everything luking fine but i click the click button it show HTTP has been stopped. I am giving logcat picture Please click this Logcat picture to see Please help me what is wrong and how to resolve this problem
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String URL_CLICK = "http://www.careoservice.goyalsoftwares.com/feedback/add.php";
public static final String KEY_MESSAGE = "message";
public static final String KEY_EMAIL = "email";
private EditText editTextMessages;
private EditText editTextEmail;
private Button buttonSend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextEmail = (EditText) findViewById(R.id.editmsg);
editTextEmail = (EditText) findViewById(R.id.editmail);
buttonSend = (Button) findViewById(R.id.btnclk);
buttonSend.setOnClickListener(this);
}
public void sendView() throws JSONException {
final String message = editTextMessages.getText().toString().trim();
final String email = editTextEmail.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_CLICK, new Response.Listener<String>() {
#Override
public void onResponse(String s) {
Toast.makeText(MainActivity.this,s, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_MESSAGE, message);
params.put(KEY_EMAIL, email);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
#Override
public void onClick(View v) {
if (v == buttonSend) {
try {
sendView();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
You are not initialazing the "editTextMessages", this is your null pointer exception.

android Volley does not response when request again

all.
I am using volley to make a http request to my website server. And it works fine and response when click the commit button to add a request to request queue. But click the button again does not response. It still shows the progressdialog and response nothing. Is there something wrong ?
Here's code example below:
public class MainActivity extends ActionBarActivity {
private static final String URL = "http://www.google.com";
public RequestQueue mQueue = null;
public StringRequest request = null;
private Button mScanButton = null;
private TextView mDisplay = null;
private ProgressDialog dialog = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mScanButton = (Button) findViewById(R.id.button_scan);
mDisplay = (TextView) findViewById(R.id.display);
request = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
System.out.println("Response retrieved");
mDisplay.setText(response);
dialog.dismiss();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mDisplay.setText("That didn't work.");
dialog.dismiss();
}
});
request.setShouldCache(false);
mScanButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity.this, "Mapping data", "please wait..");
mQueue = Volley.newRequestQueue(MainActivity.this);
mQueue.add(request);
System.out.println("Add to queue..");
}
});
}
try this May this guide you here is the complete source which helps you understand easily.
looking at this line
if (ServerConfig.isNetworkOnline(SplashScreen.this)) {
StringRequest strReq = new StringRequest(Request.Method.POST, "your server link", serverResponse, errorListener) {
};
AppController.getInstance().getRequestQueue().add(strReq);
} else {
Toast.makeText(getApplicationContext(), "Please check your network connection", Toast.LENGTH_SHORT).show();
finish();
}
you can remove those above code leaving with below code.
StringRequest strReq = new StringRequest(Request.Method.POST, "your server link", serverResponse, errorListener) {
};
AppController.getInstance().getRequestQueue().add(strReq);
I try to move the area of request declaration to on-click method and it works fine. But I still don't understand how it works.
public class MainActivity extends ActionBarActivity {
private static final String URL = "http://www.google.com";
public RequestQueue mQueue = null;
public StringRequest request = null;
private Button mScanButton = null;
private TextView mDisplay = null;
private ProgressDialog dialog = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mScanButton = (Button) findViewById(R.id.button_scan);
mDisplay = (TextView) findViewById(R.id.display);
mScanButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity.this, "Mapping data", "please wait..");
request = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
System.out.println("Response retrieved");
mDisplay.setText(response);
dialog.dismiss();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mDisplay.setText("That didn't work.");
dialog.dismiss();
}
});
mQueue = Volley.newRequestQueue(MainActivity.this);
mQueue.add(request);
System.out.println("Add to queue..");
}
});
}

Categories

Resources