java - 安卓 :-Consume a web service through Post method as a json Request and Response

标签 java android json web-services wcf

我的网络服务代码如下我正在使用 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;
}

我的客户端(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);
        }

调用 Web 服务后我得到以下输出:

The server encountered an error processing the request. See server logs for more details.

基本上我的问题是我无法使用 NameValuePair 传递值。

最佳答案

以下代码对我有用:

 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;
}

关于java - 安卓 :-Consume a web service through Post method as a json Request and Response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16520666/

相关文章:

java - hibernate cfg文件

java - 在 Java 中,匿名类与其定义的类型有什么关系?

c# - Visual Studio 2017 中的 Android 设备管理器显示 'Android SDK Platform is Missing'

android - 动画列表在 Android 5.0 (Lollipop) 中不起作用

android - Android Studio 1.3 中实验性 Gradle 构建工具的 lintOptions

python - 有没有办法读取 dataframe.to_json(orient ='table') 保存的 JSON 文件?

java - 单击按钮时面板未出现

java - 无法使用 Spring Boot 和 Jersey 2 提供静态内容

arrays - 如何使用jq将对象列表转换为列表对象?

c# - 复杂的 Json 到 C# 对象反序列化类