简体   繁体   English

Android:通过Post方法将网络服务作为json请求和响应

[英]Android :-Consume a web service through Post method as a json Request and Response

My web service code is following i am using WCF Restful webservices, 我正在使用WCF Restful网络服务,这是我的网络服务代码,

 [OperationContract]
    [WebInvoke(Method = "POST",
      ResponseFormat = WebMessageFormat.Json,
      BodyStyle = WebMessageBodyStyle.Wrapped,
      UriTemplate = "Login?parameter={parameter}")]
      string Login(string parameter);


 public string Login(string parameter)
    {



        /*
         * input :=  {"username":"kevin","password":"123demo"}
         * output:=  1=sucess,0=fail
         *
        */

        //Getting Parameters from Json  
        JObject jo = JObject.Parse(parameter);
        string username = (string)jo["username"];
        string password = (string)jo["password"];
        return ""+username;
}

my client side(Android) code is following 我的客户端(Android)代码如下

 JSONObject json = new JSONObject();
          try {
            json.put("username","demo");
            json.put("password","password123");

        HttpPost postMethod = new HttpPost(SERVICE_URI);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        postMethod.setHeader("Accept", "application/json");
        postMethod.setHeader("Content-type", "application/json");

        nameValuePairs.add(new BasicNameValuePair("parameter",""+json.toString()));
        HttpClient hc = new DefaultHttpClient();
        postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = hc.execute(postMethod);
        Log.i("response", ""+response.toString());
        HttpEntity entity = response.getEntity();
        final String responseText = EntityUtils.toString(entity);

        string=responseText;
        Log.i("Output", ""+responseText);
        } 

        catch (Exception e) {
            // TODO Auto-generated catch block
        Log.i("Exception", ""+e);
        }

I am getting following output after calling Web service: 调用Web服务后,我得到以下输出:

The server encountered an error processing the request. 服务器在处理请求时遇到错误。 See server logs for more details. 有关更多详细信息,请参见服务器日志。

Basically my problem is I am unable to pass value by using NameValuePair . 基本上我的问题是我无法使用NameValuePair传递值。

Following code worked for me: 以下代码为我工作:

 public static String getJsonData(String webServiceName,String parameter)
{  
    try  
    {
    String urlFinal=SERVICE_URI+"/"+webServiceName+"?parameter=";
    HttpPost postMethod = new HttpPost(urlFinal.trim()+""+URLEncoder.encode(parameter,"UTF-8"));
    postMethod.setHeader("Accept", "application/json");
    postMethod.setHeader("Content-type", "application/json");

    HttpClient hc = new DefaultHttpClient();

    HttpResponse response = hc.execute(postMethod);
    Log.i("response", ""+response.toString());
    HttpEntity entity = response.getEntity();
    final String responseText = EntityUtils.toString(entity);

    string=responseText;
    Log.i("Output", ""+responseText);
      }
      catch (Exception e) {
    }

return string;
}

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

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