title: how to connect android to asp
body: <p>Can we connect android to aspx page for doing some background task and getting back result? If yes then how can we do this? I have no idea about this. I have done same thing using php but for asp I dont know how to do.</p>
  
  <p>This is what I tried. I am just passing textbox value to asynctask and in asp I am reading that value and passing same value back to android.</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      EditText k;
      String text;
      Button submit;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          k=(EditText) findViewById(R.id.k);
  
          submit=(Button) findViewById(R.id.submit);
  
          submit.setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
  
                  BackgroundTask bkg=new BackgroundTask(getApplicationContext());
                  text=k.getText().toString();
                  bkg.execute(text);
  
  
              }
          });
  
  
  
  
  
  
      }
  
      public class BackgroundTask extends AsyncTask&lt;String,Void,String&gt; {
  
  
  
  
           Context ctx;  
             BackgroundTask(Context ctx)  
             {  
              this.ctx =ctx;  
             }
  
  
             @Override  
             protected void onPreExecute() { 
  
  
  
             }  
  
             protected String doInBackground(String... params) {  
  
  
              // String login_url ="http://10.0.2.2/neha/testasp.php";
               //  String login_url ="http://192.168.2.50:84/reporting_manager.php";
                 String login_url ="http://192.168.2.55:82/Default2.aspx";
  
  
  
  
  
                    final String text = params[0];  
  
                     try {  
                       URL url = new URL(login_url);  
                       HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();  
                       httpURLConnection.setRequestMethod("POST");  
                       httpURLConnection.setDoOutput(true);  
                       httpURLConnection.setDoInput(true);  
                       OutputStream outputStream = httpURLConnection.getOutputStream();  
                       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));  
                     String data = URLEncoder.encode("text","UTF-8")+"="+URLEncoder.encode(text,"UTF-8");  
                       bufferedWriter.write(data);  
                       bufferedWriter.flush();  
                       bufferedWriter.close();  
                       outputStream.close();  
                       InputStream inputStream = httpURLConnection.getInputStream();  
                       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));  
                       String response = "";  
                       String line = "";  
                       while ((line = bufferedReader.readLine())!=null)  
                       {  
                         response+= line;  
                       }  
                       bufferedReader.close();  
                       inputStream.close();  
                       httpURLConnection.disconnect();  
                       return response;  
                     } catch (MalformedURLException e) {  
                       e.printStackTrace();  
                     } catch (IOException e) {  
                       e.printStackTrace(); 
  
                       return "Not Connected!";
                     }  
                  // }  
                   return null;  
                 }  
                 @Override  
                 protected void onProgressUpdate(Void... values) {  
                   super.onProgressUpdate(values);  
                 }  
                 @Override  
                 protected void onPostExecute(String result) {  
  
                     Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  
  
  
  
                 }  
               }  
  
  
  
  
  }
  </code></pre>
  
  <p>And below is my asp code.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  public partial class Default2 : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          Label1.Text = Request.QueryString["text"].ToString();
      }
  }
  </code></pre>