简单的android和ASP.NET混合

时间:2012-10-01 12:28:53

标签: java c# android asp.net

我编写了一个将文本数据发送到asp.net webserver的android应用程序。我正在使用下一个代码通过Http发送数据:

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)表格应该读取发送给它的字符串数据并显示它。

请帮助

1 个答案:

答案 0 :(得分: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
                }
        });

    }
}