繁体   English   中英

无法将Android连接到WCF服务

[英]Unable to connect android to WCF Service

我试图将android应用程序连接到WCF服务,但无法正常工作。 WCF托管在IIS服务器上。 我不知道哪个是错误的android应用程序或WCF服务本身。 经过测试,WCF服务运行正常。 这是我的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);
}

这是我在其中访问WCF服务的android代码

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模拟器请求WCF服务时应用崩溃。 在这里的logchat。

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

引发了一个异常,但是由于您正在捕获所有异常,因此您没有详细信息。 由于某种原因,抛出的Exception没有设置消息,这就是为什么在此行上获取NullPointerException的原因

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

请发布有关实际引发的异常的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM