2012-10-01 25 views
0

我写了一个向asp.net webserver发送文本数据的android应用程序。我通过HTTP使用下面的代码来发送数据:简单的android和ASP.NET混合

try { 
    URL url = new URL(serverUrl); 
    connection = (HttpURLConnection)url.openConnection(); 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 
    connection.setUseCaches(false); 

    // Enable POST method 
    connection.setRequestMethod("POST"); 

    connection.setRequestProperty("Connection", "Keep-Alive"); 
    connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8"); 
    output = new DataOutputStream(connection.getOutputStream()); 
    String finalUri = sendedUrl.replace("gaburl", ""); 
    output.writeChars(finalUri); 
    //Toast.makeText(context, finalUri, 1000).show(); 
    output.flush(); 
    output.close(); 
} 
catch(Exception e) { 
    Toast.makeText(context, e.toString(), 5); 
} 

我怎样才能收到并在ASP.NET应用程序中显示的数据由output.writeChars(finalUri)方法发送?这个过程应该像下面的描述一样执行:

1)我们有asp.net表单,这是早期发送者的android方法desripe的目标;

2)表单应读取发送给它并显示它的字符串数据。

请帮助

回答

2
Using Ksoap2 library and write .net web service 
Sucessful Connection with Asp.net Webservice----- 
package ProductVerificationCard.in; 

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class AdminLogin extends Activity { 
    /** Called when the activity is first created. */ 
    Button btn_ok; 
    TextView textView; 
    private static final String SOAP_ACTION = "http://tempuri.org/Login"; 

    private static final String OPERATION_NAME = "Login"; 

    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/"; 

    private static final String SOAP_ADDRESS = "http://10.0.2.2/new/WebService.asmx"; 
    String s; 


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

     btn_ok=(Button) findViewById(R.id.btn_login); 
     textView=(TextView) findViewById(R.id.tv_error); 

     btn_ok.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, 
         OPERATION_NAME); 

         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
         SoapEnvelope.VER11); 
         envelope.dotNet = true; 

         envelope.setOutputSoapObject(request); 

         HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS); 

         try 

         { 

         httpTransport.call(SOAP_ACTION, envelope); 

         Object response = envelope.getResponse(); 

         //textView.setText(response.toString()); 
         s=response.toString(); 
         if(s=="true") 
         { 
          Intent intent=new Intent(AdminLogin.this,MenuForm.class); 
           startActivity(intent); 

         } 

         else 
         { 
          textView.setText("Enter Valid Username or Password"); 
         } 
         } 

         catch (Exception exception) 

         { 

         textView.setText(exception.toString()); 

         } 
       // TODO Auto-generated method stub 
       } 
     }); 

    } 
}