1

I have a simple form and i need to send it to a URL with the parameters.

Let's say I have one parameter called token and I want to send it to example.com/?token=the_token

I don't need to handle or get the output, just send it to this url.

How I can do this? I've been trying for hours to use URLCONNECTION and HTTPURLCONNECTION and nothing worked.

Since I tried many things and nothing worked, I am trying from scratch. This is it:

if (token.isEmpty()) {
                getGCMToken();
        } else {
            //Send the token to example.com/?token=token. The URL will add the token to my database with PHP.

        }

Please do not reply with DefaultHttpClient. It is deprecated.

Trying URLConnection():

if (token.isEmpty()) {
                getGCMToken();
        } else {
            //Send the tokeb to example.com/?token=token
            URLConnection connection = null;
            try {
                connection = new URL("mysite.com/send.php?id=my_id").openConnection();
            } catch (IOException e) {
                e.printStackTrace();
            }
            connection.setRequestProperty("Accept-Charset", "UTF-8");
            try {
                InputStream response = connection.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
EliKo
  • 101
  • 7

2 Answers2

0

Use URLConnection for this.

URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
0

Look, I built a whole web/http communications class last year. I'm copying you the chunk which deals with get(s):

Import at least the following:

import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStreamWriter;

try{
    String mainUrl = "<your_url>";

    String query = "?" + "<add_your_params_here>";

    URL url = new URL (mainUrl + query);

    HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();

    InputStream in = new BufferedInputStream(urlConnection.getInputStream());

    // If you want to process the response you have to do something the the in InputStream. But ommiting it here, as you said you won't use the response.   
}
catch (FileNotFoundException fnf){
        Log.e("Incorrect Url", "Resource was not found");
}
catch (Exception e){

}

And remember to add the INTERNET permission for your Android Manifest xml file: android.permission.INTERNET

Check this response for adding internet permissions: What permission do I need to access Internet from an android application?

Community
  • 1
  • 1
MarkSkayff
  • 1,334
  • 9
  • 14
  • Application doesn't crash, but it does nothing - my .php file is not being accessed and the query don't run – EliKo Oct 05 '15 at 18:28
  • I have added the imports above. Check you have at least these. – MarkSkayff Oct 05 '15 at 18:32
  • my IDE import automically all these packages, this is not the problem. – EliKo Oct 05 '15 at 18:32
  • Ok, so then the problem must be you forgot to add the INTERNET permissions in your Android Manifest xml file. You must have INTERNET permissions for the app to be open to do HTTP requests. – MarkSkayff Oct 05 '15 at 18:33
  • I have this permissions, my whole application is built on internet. I just need the same effect when user clicks the link.. the code you posted above runs like nothing accessed or clicked the link - so the code is not running. – EliKo Oct 05 '15 at 18:44
  • It won't run if you don't change some params there for sure. Otherwise this code has been extensively tested. I'm wondering how you are making the check against your PHP application. Are you loggin something in a file, how are you detecting the request server side? – MarkSkayff Oct 05 '15 at 18:52
  • Hey, I understand what happenned. There is Exception e: `catch (Exception e){ Log.e("I dont know", "Something happenned"); // This log is being displayed }` What does it mean? * There is no problem with the PHP, since when I access this URL from my Google Chrome, everything runs fine. I tested it thousand times. – EliKo Oct 05 '15 at 18:53
  • Looks like there's some sort of Exception. It's funny, but the thing is that you have to get the message from that exception. Just query the e object. Instead of adding "Something happened" add e.getMessage() and see what happened. – MarkSkayff Oct 05 '15 at 22:51