I'm working on an Android app and currently stuck to link my database to my app for registration using volley but I get the problem:
this is response:
com.android.volley.NoConnectionError:java.io.IOEXCEPTION: Cleartext HTTP traffic to 10.0.2.2 not permitted
Here is my MainActivity code:
public class MainActivity extends AppCompatActivity {
Button loginBtn;
EditText password, username;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
password = findViewById(R.id.pssword);
username = findViewById(R.id.username);
loginBtn = findViewById(R.id.check);
loginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
login();
}
});
}
void login() {
StringRequest request = new StringRequest(Request.Method.POST,
"http://10.0.2.2:80/android/test.php", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), "this is response: " + response,
Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "this is response: " + error,
Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("username", "hicham");
params.put("password", "hello");
return params;
}
};
Volley.newRequestQueue(this).add(request);
}
}
Please add the usesCleartextTraffic="true" tag in your AndroidManifest.xml accordingly to fix this issue.
<application
android:usesCleartextTraffic="true"
</application>
This issue occurs because you are trying to access an http url instead of https and there are new restrictions regarding this lately. Hence, only after adding the tag I mentioned, your HTTP requests will work.
Happy coding! :)
Related
I've given permissions for Internet access to my application and the URL I'm using is right (I can do POST requests through postman correctly). But in my application the requests fail:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String email = Email.getText().toString();
final String password = Password.getText().toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response.equalsIgnoreCase("exitoso")) {
Intent intent = new Intent(MainActivity.this, Home.class);
startActivity(intent);
finish();
} else {
Toast.makeText(MainActivity.this, "Todo mal", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Hubo un error", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> Params = new HashMap<String, String>();
Params.put("email", email);
Params.put("password", password);
return Params;
}
#Override
public Map<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=\"UTF-8");
return headers;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
});
My emulator does not give me any responses, just stays on login activity (which is the main one).
Just rebuild the app, honestly it was that now it works.
I create project in android studio and use Volley for signup and login
I can login in my android app with email and password that i insert in database with phpMyadmin But signup in android doesn't insert email and password to database!
this is my LoginDialog.java code :
public class LoginDialog extends DialogFragment {
EditText edtEmail, edtPass;
Button btnSignup, btnLogin;
OnSignupClicked onSignupClicked;
View view;
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
view = LayoutInflater.from(getContext()).inflate(R.layout.login_dialog, null);
setupViews();
builder.setView(view);
return builder.create();
}
private void setupViews() {
btnSignup = (Button) view.findViewById(R.id.btn_loginDialog_signup);
edtEmail = (EditText) view.findViewById(R.id.edt_loginDialog_email);
edtPass = (EditText) view.findViewById(R.id.edt_loginDialog_pass);
btnLogin = (Button) view.findViewById(R.id.btn_loginDialog_login);
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = edtEmail.getText().toString();
String pass = edtPass.getText().toString();
userSignup(email, pass);
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
login(edtEmail.getText().toString(), edtPass.getText().toString());
}
});
}
private void login(final String myEmail, final String pass) {
String url = "http://192.168.1.101/login.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response.equals("not found")) {
Toast.makeText(getContext(), "پست الکترونیک یا رمز عبور اشتباه است", Toast.LENGTH_SHORT).show();
} else {
onSignupClicked.onClicked(response);
dismiss();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.i("LOG", "onErrorResponse: " + error.toString());
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", myEmail);
params.put("pass", pass);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
public void setOnSignupClicked(OnSignupClicked onSignupClicked) {
this.onSignupClicked = onSignupClicked;
}
private void userSignup(final String email, final String pass) {
String url = "http://192.168.1.101/signup.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.i("LOG", "onResponse: "+response);
onSignupClicked.onClicked(response);
dismiss();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.i("LOG", "onErrorResponse: " + error.toString());
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("pass", pass);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
public interface OnSignupClicked {
void onClicked(String email);
}
}
And this my signup.php code :
<?php
include "connect.php";
$email = $_POST["email"];
$pass = $_POST["pass"];
$query = "INSERT INTO user(email,pass)VALUES (:email,:pass)";
$res=$connect->prepare($query);
$res->bindParam(":email",$email);
$res->bindParam(":pass",$pass);
$res->execute();
if($res){
echo $email;
}else{
echo "error";
}
Please guide me how can i fix signup to connect with database :X
Your server side code had bugs. I fixed it
<?php
$connection = mysqli_connect('localhost','root','password','user') or die('connecition err');
$email = $_POST['email'];
$pass = $_POST['pass'];
$sql = "INSERT INTO `user` (`email`,`pass`) VALUES ('$email','$pass')";
$query = mysqli_query($connection,$sql);
if($query){
echo $email;
}else{
echo "no";
}
?>
I am new to Android and I am using JAVA and Android studio. I have to use the WooCommerce API to get the list of all orders:
wp-json/wc/v2/orders
Is this possible using the volley library and if yes, how can I call this and get the response (I have my client_id and client_secret)?
Here is my activity page:
public class YourOrders extends AppCompatActivity {
private TextView textViewback;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_orders);
getSupportActionBar().hide();
textViewback = (TextView) findViewById(R.id.textViewback);
textViewback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}}
String url = "https://www.example.com/wp-json/wc/v2/orders?consumer_key=123&consumer_secret=abc";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
textViewback.setText("Response: " + response.toString());
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
I am new to android and using volley to communicate with web services.
Postman details
I found sending a raw text to the web service will only work on postman.I try using grant_type=password&username=myusername&password=mypassowrd getting myusername and mypassword from two editText from my mainActivity, .
Is there any better way to achieve this?
public class MainActivity extends AppCompatActivity {
Button btnLogin,btnFetch,btnExit;
EditText editTxtUsr,editTxtPass;
TextView txtView;
String uRL ="****";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin=findViewById(R.id.buttonLogin);
btnFetch=findViewById(R.id.buttonFetch);
btnExit=findViewById(R.id.buttonExit);
editTxtUsr=findViewById(R.id.editTextUsrname);
editTxtPass=findViewById(R.id.editTextPassword);
txtView=findViewById(R.id.textViewData);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String wsURL = uRL + "/authtoken";
//final String appData = "grant_type=password&username=" + editTxtUsr + "&password=" + editTxtPass;
StringRequest request = new StringRequest(Request.Method.POST, wsURL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response.equals("true")) {
Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Incorrect ", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this, "Some error occured" + volleyError, Toast.LENGTH_SHORT).show();
}
}) {
#Override
public Map<String,String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/text");
headers.put("charset", "TYPE_UTF8_CHARSET");
return headers;
}
#Override
protected Map<String,String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<>();
parameters.put("grand_type", "SET_VALUE");
parameters.put("Username", editTxtUsr.getText().toString());
parameters.put("Password", editTxtPass.getText().toString());
return parameters;
}
};
RequestQueue rQueue= Volley.newRequestQueue(MainActivity.this);
rQueue.add(request);
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
I have tried various ways to achieve this, but with no luck. That's a sample of my code that I use to try to connect to the web service.
I get [253] BasicNetwork.performRequest: Unexpected response code 400 for...
I searched thoroughly through StackOverflow but I can't seem to find the correct answer to my question.
Any help would be appreciated.
Its been days I have been working on this but after I posted my question, I found the solution after some tries.
The answer is simpler than I have thought.
What you need to do is make an objectrequest and override the body with the text you want to post. Nothing had to change in headers or parameters.
I still can't find an answer using Stringrequest.
#Override
public byte[] getBody() {
try {
return requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
Where requestBody is the raw text you want to send as a string.
I'm trying to make a http-post request to my localhost server. The problem I have Is that I'm not getting any response when I make the request.
I'm doing the request from a fragment class:
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i(TAG, " Button clickde");
fLogin.setCallback(loginButton); //Let's register a callback
final RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());
HashMap<String, String> params = new HashMap<String, String>();
params.put("token", "AbCdEfGh123456");
String url = "http://127.0.0.1/create_row.php";
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url,new JSONObject(params), new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.i(TAG, response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG, error.getMessage());
}
});
fLogin.setFacebookListener(new FacebookLogin.OnFacebookListener() {
#Override
public void onFacebookLoggedIn(JSONObject parameters) {
}
});
}
});
Nothing is printed out from the onResponse or Response.ErrorListener.
You did't add the jsObjRequest to the queue, so the request wasn't executed, add it to the queue by:
queue.add(jsObjRequest);
see the tutorial on developer.android.com.