【问题标题】:Unable to connect android to WCF Service无法将 android 连接到 WCF 服务
【发布时间】:2012-11-18 06:51:55
【问题描述】:

我试图将 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 服务时,应用程序崩溃。这里是聊天记录。

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

【问题讨论】:

  • 错误似乎是在调用 Log.d 时您确定它在此方法中出现异常,如果是,您确定在调用 Log.d("Error",e.getMessage()); 中异常消息不是空?
  • 我使用日志只是为了找出错误在哪里。通过做这个。我在 //get the response HttpResponse response = client.execute(request); 之后发现了什么不执行。我的 URI 是否正确。
  • 针对可能发生的特定异常优化您的异常处理。在这种情况下,如果e.Message() 为空,您的应用程序将在您打印堆栈跟踪之前崩溃。如果您不想缩小异常处理的范围,请至少在记录 ala if(null != e.Message()) Log.d("Error",e.getMessage()); 之前添加一个条件。这样,您至少可以获取堆栈跟踪以找出真正的问题发生在哪里。
  • 我使用了 e.toString()。我收到以下错误android.os.NetworkOnMainThreadException。我应该在不同的类中编写网络访问代码吗?如果可能,请指出正确的来源或教程。

标签: android wcf


【解决方案1】:

有一个异常被抛出,但是因为您正在捕获所有异常,所以您没有详细信息。由于某种原因,抛出的异常没有设置消息,这就是为什么你在这一行得到 NullPointerException

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

请发布更多关于真正抛出的异常的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多