如何在Android客户端中使用WCF Rest Service?

时间:2018-10-27 06:51:58

标签: android json wcf android-volley

我创建了一个简单的WCF服务,该服务返回一行的简单json。我使用了Ksoap,但是它不起作用,因为我认为ksoap可以处理基于xml的数据,但是我的服务返回的是json数据。现在,我正在尝试使用Volley在Android中使用该WCF服务。但是不知何故,它也不起作用,也不在日志上打印错误。这是我的WCF。

[ServiceContract]
    public interface IEmpInfoService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GetEmpSalary/",
            RequestFormat=WebMessageFormat.Json ,
            ResponseFormat=WebMessageFormat.Json,
            BodyStyle=WebMessageBodyStyle.Wrapped)]

        string GetEmpSalary();
    }

公共类EmpInfoService:IEmpInfoService     {

    public string GetEmpSalary()
    {

        return "Hello from WCF";
    }
}

Android实现:

public class MainActivity extends AppCompatActivity {
   public static final String URL = "http://192.168.0.xxx/EmployeeService/EmpInfoService.svc/GetEmpSalary";

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

        StringRequest request = new StringRequest(URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("CODE" , response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MainActivity.this, "WRONG", Toast.LENGTH_SHORT).show();
            }
        });

        // vOLLEY qUEUE
        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(request);
    }
}

0 个答案:

没有答案