iT邦幫忙

0

Android 無法連到WCF

  • 分享至 

  • xImage

我使用Eclipse開發一支App要連接WCF
用夜神模擬器測試

package com.example.test;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;


public class MainActivity extends Activity {

	private Button btn;  
    private TextView resultView;  
    private String result="";  
    @SuppressLint("NewApi")  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        btn = (Button)findViewById(R.id.btn);  
        resultView = (TextView)findViewById(R.id.textView);  
          
        btn.setOnClickListener(new OnClickListener() {  
            public void onClick(View arg0) {  
                new Thread(){    
                    @Override    
                    public void run() {    
                        // TODO Auto-generated method stub    
                        super.run();    
                        String studentNum = "mbmbmbmb";  

                        result = getRemoteInfo(studentNum);  
                        handler.sendEmptyMessage(0);  
                    }    
                }.start();               
            }  
              
        });  
    }  
      
    private String getRemoteInfo(String studentNum){  
        // 命名空间  
            String nameSpace = "http://tempuri.org/";  
            // 調用的名稱方法  
            String methodName = "HelloWorld";  
            // EndPoint  
            String endPoint = "http://172.16.2.43:80/Service1.svc";  
            // SOAP Action  
            String soapAction = "http://tempuri.org/IService1/HelloWorld";  
  
            // 指定WebService的命名空間和調用的方法名  
            SoapObject rpc = new SoapObject(nameSpace, methodName);  
  
            // 設置需調用WebService接口需要傳入的兩個參數  
            rpc.addProperty("_name", studentNum);  
  
            // 生成調用WebService方法的SOAP請求信息,並指定SOAP的版本 
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);  
              
              
              
            // 設置是否調用的是dotNet開發的WebService
            envelope.dotNet = true;  
            // 等价于envelope.bodyOut = rpc;  
            envelope.setOutputSoapObject(rpc);  
  
            HttpTransportSE transport = new HttpTransportSE(endPoint);  
            transport.debug = true;  
  
                // 調用WebService  
            try {  
                transport.call(soapAction, envelope);  
            } catch (Exception e) {  
                  
            }   
  
  
            // 獲取返回的數據  
            SoapObject object = (SoapObject) envelope.bodyIn;  
            // 獲取返回的結果
  
            String result = object.getProperty(0).toString();  
  
            return result;  
    }  
  
      
    private Handler handler = new Handler() {    
            
        public void handleMessage(Message msg) {    
            super.handleMessage(msg);  
            resultView.setText(result);  
        };    
    };    
      
}

但出現例外java.lang.NullPointerException

01-05 15:27:32.943: D/dalvikvm(8130): JIT code cache reset in 0 ms (0 bytes 1/0)
01-05 15:27:32.943: D/dalvikvm(8130): GC_FOR_ALLOC freed 83K, 5% free 3053K/3212K, paused 5ms, total 5ms
01-05 15:27:32.943: D/dalvikvm(8130): GC_FOR_ALLOC freed 351K, 12% free 3159K/3588K, paused 2ms, total 2ms
01-05 15:27:40.033: W/dalvikvm(8130): threadid=10: thread exiting with uncaught exception (group=0x94c33b20)
01-05 15:27:40.033: E/AndroidRuntime(8130): FATAL EXCEPTION: Thread-215
01-05 15:27:40.033: E/AndroidRuntime(8130): Process: com.example.test, PID: 8130
01-05 15:27:40.033: E/AndroidRuntime(8130): java.lang.NullPointerException
01-05 15:27:40.033: E/AndroidRuntime(8130): at com.example.test.MainActivity.getRemoteInfo(MainActivity.java:91)
01-05 15:27:40.033: E/AndroidRuntime(8130): at com.example.test.MainActivity.access$2(MainActivity.java:51)
01-05 15:27:40.033: E/AndroidRuntime(8130): at com.example.test.MainActivity$2$1.run(MainActivity.java:42)

但不知道是怎麼發生的
WCF的部分

namespace WcfService1
{
   
    [ServiceContract(Namespace = "http://tempuri.org/")]
    public interface IService1
    {      
        [OperationContract]
        string HelloWorld(string _name);                
    }
namespace WcfService1
{
    // 注意: 您可以使用 [重構] 功能表上的 [重新命名] 命令同時變更程式碼、svc 和組態檔中的類別名稱 "Service1"。
    // 注意: 若要啟動 WCF 測試用戶端以便測試此服務,請在 [方案總管] 中選取 Service1.svc 或 Service1.svc.cs,然後開始偵錯。
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {        
        public string HelloWorld(string _name)
        {
            return _name + " hello world!";
        }        
    }
}

就簡單的回傳值
模擬器輸入網址也連的到
https://ithelp.ithome.com.tw/upload/images/20180105/201080616T8jMadXMV.png
不知道哪裡有問題請各位大大幫幫忙

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答