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?
Related
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
so i have this requestqueue like this with 'com.android.volley:volley:1.1.1' and permission.INTERNET
public TextView txt;
public String text="";
private static final String
url="http://192.168.100.7/diari/tampil_penyakit.php";
public RequestQueue requestQueue;
public StringRequest stringRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.txt);
requestQueue= Volley.newRequestQueue(MainActivity.this);
stringRequest= new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
text=response;
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
text="Error";
}
});
requestQueue.add(stringRequest);
text=text+" empty";
txt.setText(text);
}
but the txt only show "empty" and i already tried the url with postman, can somebody help me
Change your onResponse to this:
stringRequest= new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
text=response;
text=text+" empty";
txt.setText(text);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
text="Error";
}
It wasn't working because what you were basically doing was declaring the variable text like text=""; then setting up your queue, after setting it up setting your textview's text with your empty variable without waiting for a response from the server.
I'm new to android development. I'm trying to send some data to server using volley. But it is not sending parameters. Please help. The server is saying that the parameter is not set. I checked with the isset in php. When I tried sending data from a html form, it's working. But the volley is not sending the parameters.
public class MainActivity extends AppCompatActivity {
EditText uname;
EditText uotp;
RequestQueue myqueue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname = (EditText) findViewById(R.id.uname);
uotp = (EditText) findViewById(R.id.uotp);
myqueue = Volley.newRequestQueue(this);
}
public void sendotp(View v)
{
String unameval = uname.getText().toString();
if(unameval.matches(""))
{
Toast.makeText(getApplicationContext(), "Enter Phone Number", Toast.LENGTH_SHORT).show();
}
else
{
HashMap<String, String> params = new HashMap<>();
params.put("phone", unameval);
String myUrl = "http://privateurlhere";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, myUrl, new JSONObject(params), new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
String dispname = response.getString("stat");
Toast.makeText(getApplicationContext(), dispname, Toast.LENGTH_SHORT).show();
}
catch(JSONException e)
{
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Resp err" + error.toString(), Toast.LENGTH_SHORT).show();
}
});
myqueue.add(request);
}
}
}
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
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.