通过SOAP将数据从android传递到Backend

时间:2014-08-20 13:17:14

标签: android web-services soap

我必须使用soap为动态后端创建费用。我目前在同一个网络上,所以还不需要身份验证。该服务将(日期,金额,货币,用户)作为参数,但我似乎甚至无法连接到Web服务。任何人都可以帮助我吗?

MyActivity

import com.example.KsoapExample.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MyActivity extends Activity {
private TextView txt;
private String celsius;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final EditText edt = (EditText) findViewById(R.id.value_to_convert);
    final EditText edt2 = (EditText) findViewById(R.id.amount);
    final EditText edt3 = (EditText) findViewById(R.id.user);
    Button btn = (Button) findViewById(R.id.submit);
    txt = (TextView) findViewById(R.id.answer);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (edt.length() > 0) {
                createRecord(edt.getText().toString(), Integer
                        .parseInt(edt2.getText().toString()), edt3
                        .getText().toString());
            } else {
                txt.setText("values can not be empty.");
            }
        }
    });
}

public Handler handler = new Handler(new Handler.Callback() {

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {

        case 0:
            txt.setText(celsius);
            break;
        }
        return false;
    }
});

private final void createRecord(final String date, final int i,
        final String user) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            SoapRequests ex = new SoapRequests();
            celsius = ex.getResponse(date, i, user);
            handler.sendEmptyMessage(0);
        }
    }).start();
}
}

SoapRequests活动

    package com.example.KsoapExample;

import android.util.Log;
import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import java.io.IOException;
import java.net.Proxy;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

public class SoapRequests {

    private static final boolean DEBUG_SOAP_REQUEST_RESPONSE = true;
    private static final String MAIN_REQUEST_URL = "LOCAL HOST";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String SOAP_ACTION = "http://tempuri.org/";
    private static String SESSION_ID;

    private final void testHttpResponse(HttpTransportSE ht) {
        ht.debug = DEBUG_SOAP_REQUEST_RESPONSE;
        if (DEBUG_SOAP_REQUEST_RESPONSE) {
            Log.v("SOAP RETURN", "Request XML:\n" + ht.requestDump);
            Log.v("SOAP RETURN", "\n\n\nResponse XML:\n" + ht.responseDump);
        }
    }

    public String getResponse(String date, int expense, String user) {
        String data = null;
        String methodname = "createRecord";

        SoapObject request = new SoapObject(NAMESPACE, methodname);
        request.addProperty("Date", date);
        request.addProperty("Expense", expense);
        request.addProperty("User", user);

        SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);

        HttpTransportSE ht = getHttpTransportSE();
        try {
            ht.call(SOAP_ACTION + methodname, envelope);
            testHttpResponse(ht);
            SoapPrimitive resultsString = (SoapPrimitive) envelope
                    .getResponse();

            @SuppressWarnings("unchecked")
            List<HeaderProperty> COOKIE_HEADER = (List<HeaderProperty>) ht
                    .getServiceConnection().getResponseProperties();

            for (int i = 0; i < COOKIE_HEADER.size(); i++) {
                String key = COOKIE_HEADER.get(i).getKey();
                String value = COOKIE_HEADER.get(i).getValue();

                if (key != null && key.equalsIgnoreCase("set-cookie")) {
                    SoapRequests.SESSION_ID = value.trim();
                    Log.v("SOAP RETURN", "Cookie :" + SoapRequests.SESSION_ID);
                    break;
                }
            }
            data = resultsString.toString();

        } catch (SocketTimeoutException t) {
            t.printStackTrace();
        } catch (IOException i) {
            i.printStackTrace();
        } catch (Exception q) {
            q.printStackTrace();
        }
        return data;
    }

    private final SoapSerializationEnvelope getSoapSerializationEnvelope(
            SoapObject request) {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.implicitTypes = true;
        envelope.setAddAdornments(false);
        envelope.setOutputSoapObject(request);
        return envelope;
    }

    private final HttpTransportSE getHttpTransportSE() {
        HttpTransportSE ht = new HttpTransportSE(Proxy.NO_PROXY,
                MAIN_REQUEST_URL, 60000);
        ht.debug = true;
        ht.setXmlVersionTag("<?xml version=\"1.0\" encoding= \"UTF-8\" ?>");
        return ht;
    }

    @SuppressWarnings("unused")
    private final List<HeaderProperty> getHeader() {
        List<HeaderProperty> header = new ArrayList<HeaderProperty>();
        HeaderProperty headerPropertyObj = new HeaderProperty("cookie",
                SoapRequests.SESSION_ID);
        header.add(headerPropertyObj);
        return header;
    }
}

MyActivity的XML

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

    <EditText
        android:id="@+id/value_to_convert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:hint="@string/date"
        android:gravity="center"
        android:inputType="number|text" />

    <EditText
        android:id="@+id/amount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="center"
        android:hint="@string/amount"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="center"
        android:hint="@string/user"
        android:inputType="textPersonName" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/submit"
        android:id="@+id/submit"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="30sp"
        android:id="@+id/answer"
        android:hint="@string/response"
        />
</LinearLayout>

0 个答案:

没有答案