2012-11-18 101 views
0

我正試圖將android應用程序連接到WCF服務,但它不工作。 WCF託管在IIS服務器上。我不知道哪一個是錯誤的android應用程序或WCF服務本身。 WCF服務在測試時工作正常。這是我的WCF服務代碼。無法連接android到WCF服務

[ServiceContract(Namespace = "http://services.example.com")] 
public interface IEmployeeInfo 
{ 
    [OperationContract] 
    [WebInvoke(
     BodyStyle = WebMessageBodyStyle.Wrapped, 
     Method="Get", 
     ResponseFormat = WebMessageFormat.Json, 
     UriTemplate = "GetEmployee/?key={employeeId}")] 
    Employee GetEmployee(int employeeId); 
} 

這是我的Android代碼在我訪問WCF服務

try { 

     DefaultHttpClient client = new DefaultHttpClient(); 
     // http get request 

     HttpGet request = new HttpGet(EMPLOYEE_SERVICE_URI + evEmployeeId.getText()); 
     Log.d("Connect","Connecting to Server 0"); 
     Log.d("Connect","Connecting to Server 1"); 
     // set the hedear to get the data in JSON formate 
     request.setHeader("Accept", "application/json"); 
     request.setHeader("Content-type", "application/json"); 

     //get the response 
     HttpResponse response = client.execute(request); 

     HttpEntity entity = response.getEntity(); 

     //if entity contect lenght 0, means no employee exist in the system with these code 
     if(entity.getContentLength() != 0) { 
      // stream reader object 
      Reader employeeReader = new InputStreamReader(response.getEntity().getContent()); 
      //create a buffer to fill if from reader 
      char[] buffer = new char[(int) response.getEntity().getContentLength()]; 
      //fill the buffer by the help of reader 
      employeeReader.read(buffer); 
      //close the reader streams 
      employeeReader.close(); 
      Log.d("Connect","Connecting to Server 2"); 
      //for the employee json object 
      JSONObject employee = new JSONObject(new String(buffer)); 

      //set the text of text view 
      tvEmployeeCode.setText("Code: " + employee.getString("EmployeeId")); 
      tvName.setText("Name: " + employee.getString("FirstName") + " " + employee.getString("LastName")); 
      tvAddress.setText("Address: " + employee.getString("Address")); 
      tvBloodGroup.setText("Blood Group: " + employee.getString("BloodGroup")); 


     } 
     else { 

      text.setTextSize(R.string.text); 

     } 

    } 
    catch (Exception e) { 

    Log.d("Error",e.getMessage()); 
e.printStackTrace(); 
} 

任何幫助表示讚賞。

當我從android模擬器applcation崩潰請求WCF服務。這裏是聊天室。

11-17 22:56:55.566: W/dalvikvm(1174): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
11-17 22:56:55.616: E/AndroidRuntime(1174): FATAL EXCEPTION: main 
11-17 22:56:55.616: E/AndroidRuntime(1174): java.lang.NullPointerException: println needs a message 
11-17 22:56:55.616: E/AndroidRuntime(1174):  at android.util.Log.println_native(Native Method) 
11-17 22:56:55.616: E/AndroidRuntime(1174):  at android.util.Log.d(Log.java:138) 
11-17 22:56:55.616: E/AndroidRuntime(1174):  at com.yyousuf.sample.EmployeeInfoActivity.onClick 
+0

那麼,*什麼*不工作? –

+0

錯誤似乎是在調用'Log.d'你確定它在這個方法中是異常的,如果你確定在調用Log.d(「Error」,e.getMessage() );'異常消息不爲空? –

+0

我使用日誌只是爲了找出錯誤在哪裏。通過做這個。 //得到迴應 HttpResponse response = client.execute(request); 未執行。我的URI是否正確。 – user1435242

回答

0

有一個異常被拋出,但因爲你正在捕獲所有異常,所以你沒有具體的細節。出於某種原因,扔異常沒有消息集,這就是爲什麼你在這行

Log.d("Error",e.getMessage()); 

得到NullPointerException異常請張貼關於真實扔異常的更多信息。

相關問題