Search code examples
androidandroid-asynctaskget

Sending values when I make a get request in HttpAsyncTask


I have a get request with querystrings that runs ok. But when I add a querystring from type:

public String getSsid()
{
    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    return wifiInfo.getSSID();//I add the return value
}

The get the petition does not works with this type of data (when I use getSystemService(TELEPHONY_SERVICE) does not works too). With variables obtained otherwise it works.

The permissions of the AndroidManifest are ok.

This is my code:

public class MenuActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        Bundle bundle = this.getIntent().getExtras();
        ((MyApplication) this.getApplication()).setAppUser(bundle.getString("email"));

        String email= ((MyApplication) this.getApplication()).getAppUser();
        String ssid = getSsid();

        //IF I REMOVE THE SSID AND LEAVE THE EMAIL IT WORKS!!
        String mUrl = "http://www.MyUrl.com/app/get.aspx?appId=0001&email=" + email + "&ssid" +ssid;

        new HttpAsyncTask().execute(mUrl);               
    }

    private class HttpAsyncTask extends AsyncTask<String, Void, String>
    {
        @Override
        protected String doInBackground(String... urls)
        {
            return GET(urls[0]);
        }
    }

    public static String GET(String url)
    {
        InputStream inputStream = null;
        String result = "";

        try
        {
            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            // make GET request to the given URL
            org.apache.http.HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // convert inputstream to string

            if(inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";

        }
        catch (Exception e)
        {
            Log.d("InputStream", e.getLocalizedMessage());
        }

        return result;
    }

    private static String convertInputStreamToString(InputStream inputStream) throws IOException
    {
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line = "";
        String result = "";

        while((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();

        return result;
    }

    public String getSsid()
    {
        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

        return wifiInfo.getSSID();
    }
}

Solution

  • You are providing a wrong query string. You missed an "equal to" sign.

    It should be

    String mUrl = "http://www.MyUrl.com/app/get.aspx?appId=0001&email="+ email + "&ssid=" +ssid;