0

I am developing an android application and i need to access the server side which is done as web pages in asp.net

below is the web page URL :

theWebPageURL?action=methodName&email=theEmail

i don't know what methods shall i use to access this URL and send the email parameter to it and get the response.

i searched alot and none worked

can anyone help me please ?

Rana Osama
  • 1,313
  • 2
  • 17
  • 27

2 Answers2

1

You need to use http get request HttpGet

and add this line to your manifest file

<uses-permission android:name="android.permission.INTERNET" />

also, check this link

Nikola Ninkovic
  • 1,252
  • 1
  • 12
  • 27
1

I would recommend reviewing these two similar qustions:

Make an HTTP request with android

How to add parameters to a HTTP GET request in Android?


UPDATE

The below code is a working sample I put together based off of the answers in the two links above; if this helps you, be sure to thank them.

For demonstration, the uri in this sample is being constructed into http://www.google.com/search?q=android.

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Construct the URI
        String uri = "http://www.google.com/search?";       
        List<NameValuePair> params = new LinkedList<NameValuePair>();       
        params.add(new BasicNameValuePair("q", "android"));             
        uri += URLEncodedUtils.format(params, "utf-8");

        // Run the HTTP request asynchronously
        new RequestTask().execute(uri);    
    }

    class RequestTask extends AsyncTask<String, String, String>{

        @Override
        protected String doInBackground(String... uri) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response;
            String responseString = null;
            try {
                response = httpclient.execute(new HttpGet(uri[0]));
                StatusLine statusLine = response.getStatusLine();
                if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    response.getEntity().writeTo(out);
                    out.close();
                    responseString = out.toString();
                } else{
                    //Closes the connection.
                    response.getEntity().getContent().close();
                    throw new IOException(statusLine.getReasonPhrase());
                }
            } catch (ClientProtocolException e) {
                //TODO Handle problems..
            } catch (IOException e) {
                //TODO Handle problems..
            }
            return responseString;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);                
            // result contains the response string.    
        }
    }
}

And, of course, don't forget to add this to your manifest:

<uses-permission android:name="android.permission.INTERNET" />
Community
  • 1
  • 1
geaw35
  • 2,297
  • 3
  • 18
  • 21