在“在Android中调用Web服务”时遇到问题

时间:2011-09-07 18:57:31

标签: android web-services

package com.android.project;

import android.app.Activity;
import android.os.Bundle;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import com.android.project.R;
import android.app.*;
import android.os.*;
import android.widget.TextView;


/*
 NOTE: You have to give this app permission to access
 the internet !!! See the AndroidManifest.xml file
 */
public class MyprojectActivity extends Activity {

    /** Called when the activity is first created. */
    private static final String SOAP_ACTION = "http://mydomain.org/SqlConnection";
    private static final String METHOD_NAME = "SqlConnection";
    private static final String NAMESPACE = "http://localhost/mydomain.org/";
    private static final String URL = "http://localhost:8080/WebService.asmx";
    TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv =(TextView)findViewById(R.id.text1);

        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            //request.addProperty("prop1", "myprop");

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

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object result = (Object)envelope.getResponse();

            String[] results = (String[])  result;
            tv.setText( ""+results[0]);
        }
        catch (Exception e) {
            tv.setText(e.getMessage());
        }
    }
}

2 个答案:

答案 0 :(得分:0)

首先,声明你的TextView tv private。 其次,你需要对你的问题更具体一点吗?你得到的错误是什么?

答案 1 :(得分:0)

确保在布局文件中正确写入text1

<TextView android:id="@+id/text1".....

您的评论

  

我在(R.id.text1)中有错误......为什么会这样?我没有得到它请尽快帮助。 ! - shumaila