程序不返回任何值

时间:2012-06-11 17:23:58

标签: android web-services return

我是Android编程新手。刚刚编写了这段代码,通过Web服务将kg转换为克。该程序从用户获取输入并返回文本框中的值,但遗憾的是我没有得到任何值。请帮忙。

package com.example.Passing;

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


public class PassingActivity extends Activity
{
private final String NAMESPACE = "http://www.webserviceX.NET/";
private final String URL = "http://www.webservicex.net/ConvertWeight.asmx";
private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight";
private final String METHOD_NAME = "ConvertWeight";

/** Called when the activity is first created. */

Button   mButton;
EditText mEdit;

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

    mButton = (Button)findViewById(R.id.button);
    mEdit   = (EditText)findViewById(R.id.editText);

    mButton.setOnClickListener(

            new View.OnClickListener()
            {

                public void onClick(View view)
                {
                    String weight = mEdit.getText().toString();

                    String fromUnit = "Kilograms";
                    String toUnit = "Grams";

                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 


                    PropertyInfo weightProp =new PropertyInfo();
                    weightProp.setName("Weight");
                    weightProp.setValue(weight);
                    weightProp.setType(double.class);
                    request.addProperty(weightProp);            

                    PropertyInfo fromProp =new PropertyInfo();
                    fromProp.setName("FromUnit");
                    fromProp.setValue(fromUnit);
                    fromProp.setType(String.class);
                    request.addProperty(fromProp);

                    PropertyInfo toProp =new PropertyInfo();
                    toProp.setName("ToUnit");
                    toProp.setValue(toUnit);
                    toProp.setType(String.class);
                    request.addProperty(toProp);


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

                    try 
                    {
                        androidHttpTransport.call(SOAP_ACTION, envelope);
                        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                        Log.i("myApp", response.toString());

                        EditText et = (EditText)findViewById(R.id.my_edit);
                        et.setText("equal"+response.toString());


                        /*  TextView tv = new TextView(this);
                        tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit);
                        setContentView(tv);*/

                    } 
                    catch (Exception e) 
                    {
                        e.printStackTrace();
                    }
                }
            });
    }}

main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<EditText 
    android:layout_margin="20dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minLines="15"
    android:maxLines="15"
    android:textSize="12sp"
    android:editable="false"
    android:id="@+id/my_edit"/>


<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Convert" />

2 个答案:

答案 0 :(得分:0)

我不熟悉你正在使用的kSoap lib。 但是,您正在调用Web服务,该服务需要一些时间来处理,然后立即尝试获取返回值。这看起来有问题。这通常的工作方式是你称之为Web服务,然后会有一些回调,一旦你的服务返回你的数据就会被执行。

答案 1 :(得分:0)

SoapPrimitive并不总是正常工作。尝试更改以下内容的响应方式:SoapObject result_xml = (SoapObject) envelope.bodyIn;

而不是SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

修改 这是我使用和完美工作的代码(我使用Fragment但您可以在Activity中执行相同的操作但请记住任何长度操作(如互联网,因为它们可能导致超时或滞后)如果您不想像着名的ANR那样产生不必要的副作用,那么必须才能在线程中完成。

    public class TestFragment extends Fragment implements OnClickListener{

    private final String NAMESPACE = "http://www.webserviceX.NET/";
    private final String URL = "http://www.webservicex.net/ConvertWeight.asmx";
    private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight";
    private final String METHOD_NAME = "ConvertWeight";

    private EditText textToConvert;
    private String weight;
    private Button send;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final View view = getView();
        textToConvert = (EditText) view.findViewById(R.id.ammount);
        send = (Button) view.findViewById(R.id.button);
        send.setOnClickListener(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate( R.layout.main, container, false );
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button:
            weight = textToConvert.getText().toString();
            new AsyncSendData().execute();
            break;
        default:
            break;
        }
    }


    public class AsyncSendData extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... params) {
            String fromUnit = "Kilograms";
            String toUnit = "Grams";

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
            PropertyInfo weightProp =new PropertyInfo();
            weightProp.setName("Weight");
            weightProp.setValue(weight);
            weightProp.setType(double.class);
            request.addProperty(weightProp);            

            PropertyInfo fromProp =new PropertyInfo();
            fromProp.setName("FromUnit");
            fromProp.setValue(fromUnit);
            fromProp.setType(String.class);
            request.addProperty(fromProp);

            PropertyInfo toProp =new PropertyInfo();
            toProp.setName("ToUnit");
            toProp.setValue(toUnit);
            toProp.setType(String.class);
            request.addProperty(toProp);

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

            try 
            {
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapObject result_xml = (SoapObject) envelope.bodyIn;
                Log.e("myApp", result_xml.toString());
            } 
            catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }
    }

}

这是我使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="From" />

    <EditText
        android:id="@+id/ammount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:inputType="number">
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

所以在post执行方法中你可以更新你的视图......