2012-04-17 5 views
2

ksoap2 lib와 함께 .net SOAP 웹 서비스를 사용하려고합니다. 예 : http://www.vimeo.com/9633556은 올바른 방법을 보여줍니다. 아래 예제의 코드입니다. 모든 소리 작업 괜찮아! 하지만 나는 힘을 얻는다 Close error! 여기ksoap2 lib가 포함 된 SOAP 웹 서비스

내 코드 :

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 android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

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

    TextView TV ; 
    TV=(TextView)findViewById(R.id.textView1); 
    TV.setText("Hi"); 

    String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit"; 
    String METHOD_NAME = "CelsiusToFahrenheit"; 
    String NAMESPACE = "http://tempuri.org/"; 
    String URL = "http://www.w3schools.com/webservices/tempconvert.asmx"; 

    System.setProperty("http.keepAlive", "false"); 

    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 
    Request.addProperty("Celsius", "32"); 

    SoapSerializationEnvelope soapEnvelope = new      SoapSerializationEnvelope(SoapEnvelope.VER11); 
    soapEnvelope.dotNet = true; 
    soapEnvelope.setOutputSoapObject(Request); 

    HttpTransportSE httpTransport = new HttpTransportSE(URL); 
    try 
    { 
     httpTransport.call(SOAP_ACTION, soapEnvelope); 
     SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse(); 
     TV.setText(resultString.toString()); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 

    } 
    } 

도와주세요 meeeeee

+1

stacktrace 게시 – njzk2

답변

0
public String SOAP_ACTION; 
public String METHOD_NAME; 
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/"; 
public final String SOAP_ADDRESS = "http://www.w3schools.com/webservices/tempconvert.asmx"; 
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/"; 

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

    input = (EditText) findViewById(R.id.editText1); 
    result = (EditText) findViewById(R.id.editText2); 

    cel= (RadioButton) findViewById(R.id.radioButton1); 
    fah= (RadioButton) findViewById(R.id.radioButton2); 

    rGroup = (RadioGroup) findViewById(R.id.rgTemp); 

    open = (Button) findViewById(R.id.button1); 

    open.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      Intent openScreen = new Intent("com.loginworks.demo.ANDROIDPLOT"); 

      startActivity(openScreen); 
     } 
    }); 

    rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     public void onCheckedChanged(RadioGroup rGroup, int checkedId) { 

     String in = input.getText().toString(); 

      if (cel.isChecked()) { 
       PROPERTY_NAME = "Fahrenheit"; 
       METHOD_NAME = "FahrenheitToCelsius"; 
       SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";     
      } 
      else { 
       PROPERTY_NAME = "Celsius"; 
       METHOD_NAME = "CelsiusToFahrenheit"; 
       SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit"; 
      } 

      Convert(in); 

     }    
    });   
} 

public void Convert(String val) { 
    SoapObject requestddr = new SoapObject(WSDL_TARGET_NAMESPACE, "Vrati"); 

    PropertyInfo pi=new PropertyInfo(); 

     pi.setName(PROPERTY_NAME); 
      pi.setValue(val); 
      pi.setType(String.class); 
      request.addProperty(pi); 

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

     envelope.setOutputSoapObject(request); 

     HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS); 

     Object response= null; 

     try { 

     httpTransport.call(SOAP_ACTION, envelope); 
     response = envelope.getResponse(); 

      } 
     catch (Exception exception) { 

      response=exception;     
      } 

     result.setText(response.toString()); 

http://www.loginworks.com/technical-blogs/258-working-with-soap-web-service-through-android

2

코드는 정확하지만 V4의 ICS/JB를 사용하는 경우에만 안드로이드 2에서 작업 할 수 있습니다 AsyncTask를 사용해야합니다.

package com.example.wscall3; 

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 android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 

public class MainActivity extends Activity { 
    private String TAG ="Vik"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     AsyncCallWS task = new AsyncCallWS(); 
     task.execute(); 
    } 

    private class AsyncCallWS extends AsyncTask<Void, Void, Void> { 
     @Override 
     protected Void doInBackground(Void... params) { 
      Log.i(TAG, "doInBackground"); 
      calculate(); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      Log.i(TAG, "onPostExecute"); 
     } 

     @Override 
     protected void onPreExecute() { 
      Log.i(TAG, "onPreExecute"); 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
      Log.i(TAG, "onProgressUpdate"); 
     } 

    } 

    public void calculate() 
    { 
     String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit"; 
     String METHOD_NAME = "CelsiusToFahrenheit"; 
     String NAMESPACE = "http://tempuri.org/"; 
     String URL = "http://www.w3schools.com/webservices/tempconvert.asmx"; 

     try { 
      SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 
      Request.addProperty("Celsius", "32"); 

      SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      soapEnvelope.dotNet = true; 
      soapEnvelope.setOutputSoapObject(Request); 

      HttpTransportSE transport= new HttpTransportSE(URL); 

      transport.call(SOAP_ACTION, soapEnvelope); 
      SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse(); 

      Log.i(TAG, "Result Celsius: " + resultString); 
     } 
     catch(Exception ex) { 
      Log.e(TAG, "Error: " + ex.getMessage()); 
     } 

     SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius"; 
     METHOD_NAME = "FahrenheitToCelsius"; 
     try { 
      SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 
      Request.addProperty("Fahrenheit", "100"); 

      SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      soapEnvelope.dotNet = true; 
      soapEnvelope.setOutputSoapObject(Request); 

      HttpTransportSE transport= new HttpTransportSE(URL); 

      transport.call(SOAP_ACTION, soapEnvelope); 
      SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse(); 

      Log.i(TAG, "Result Fahrenheit: " + resultString); 
     } 
     catch(Exception ex) { 
      Log.e(TAG, "Error: " + ex.getMessage()); 
     } 
    } 

} 

내의 AndroidManifest.xml은 다음과 같습니다

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.wscall3" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="7" 
     android:targetSdkVersion="16" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.wscall3.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
0

Connecting to the Network from developer.android.com :

네트워크 작업은 예상치 못한 지연을 포함 할 수있다. 이로 인해 사용자 경험이 저하되지 않도록하려면 UI의 별도 스레드에서 항상 네트워크 작업을 수행하십시오. AsyncTask 클래스는 UI 스레드에서 새 작업을 시작하는 가장 간단한 방법 중 하나를 제공합니다. 당신이 별도의 스레드에서 네트워크 운영을 (같은 httpTransport.call(SOAP_ACTION, soapEnvelope);) 수행하지 않으면

, 당신은 안드로이드 OS에 의해 강제로 닫기 대화 상자를 얻을 것이다.

AsyncTask, Service 또는 IntentService을 사용할 수 있습니다. 웹 서비스 호출을 만들기 위해 이러한 클래스 중 하나를 확장하는 다른 클래스를 작성하면 문제가 해결됩니다.