如何将android连接到asp

时间:2016-03-17 13:14:18

标签: android asp.net

我们可以将android连接到aspx页面来执行一些后台任务并获取结果吗?如果是,那我们怎么做呢?我不知道这个。我使用php做了同样的事情但是对于asp我不知道该怎么做。

这就是我尝试过的。我只是将文本框值传递给asynctask而在asp中我正在读取该值并将相同的值传递回android。

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<String,Void,String> {




         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();




               }  
             }  




}

以下是我的asp代码。

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();
    }
}

0 个答案:

没有答案