Reputation: 117
I know that there are lots of threads like this, I searched like crazy and read most of them, but can't find an answer that really helps me and works.
What I want to do is very simple, I just want to send 2 values using GET and save them in MySql.
I already have the PHP file created and is working (I tested it manually in chrome), and I have this code in Android Studio:
try {
URL url = new URL("http://myWebsite.com/connection.php");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("value1","blabla");
urlConnection.setRequestProperty("value2","blabla");
} catch(Exception e){
Log.d("Exception",e.toString());
}
I don't know what else to try, something like this should be very simple, but I can't just find a solution.
Thanks!!
Edit: I also tried:
try {
URL url = new URL("http://myWebsite.com/connection.php?value1=blabla&value2=blubla");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
} catch(Exception e){
Log.d("Exception",e.toString());
}
Edit 2: Added this to my code:
private class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("http://mySite.tk/connection.php?value1=blabla&value2=blubla");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
Log.d("Progress","Sending..");
urlConnection.connect();
Log.d("Progress","Sent!");
} catch(Exception e){
Log.d("Message",e.toString());
}
return null;
}
}
Now Im calling " new MyTask().execute()" when clicking a button, but is not working :(
Upvotes: 1
Views: 103
Reputation: 164
you can easily send GET Requests with the Google's library called Volley, it's been designed for request on Android
I recommand you to consider using Volley for your futur requests, here is an example of a GET request :
final String url = "http://yourScript.com/scipt.php";
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// display response
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", response);
}
}
){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<>();
map.put("parameter1", param1);
map.put("parameter2", param2);
return map;
}
};
// add it to the RequestQueue
queue.add(getRequest);
Upvotes: 1
Reputation: 93668
Get values aren't sent as properties. They're sent as part of the URL. http://example.com/myurl?variable1=value1&variable2=value2
Upvotes: 1