【问题标题】:C# reading information from AndroidC# 从 Android 读取信息
【发布时间】:2015-07-29 19:54:12
【问题描述】:

我尝试做一个简单的登录功能。 登录将由应用程序进行,信息将发送到 WebService(在 C# 中)。

我的应用程序通过 HttpPost 将信息发送到服务器。但我无法在 Web 服务端获取和返回此信息

为了提出我正在使用的请求(android 端):

// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", user.getText().toString()));
params.add(new BasicNameValuePair("password", pass.getText().toString()));

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

在 WebService 方面,我尝试使用 Serialize 方法,但它不起作用

Ps.:为了测试,我尝试在另一个 WebService 上运行(这个是用 PHP 构建的),并且工作正常。

任何想法如何使这项工作?

[编辑]

这是网络服务端:

[HttpPost]
public string LogarAplicativo()
{
    //Request.InputStream.Seek(0, SeekOrigin.Begin);
    string jsonData = new StreamReader(Request.InputStream).ReadToEnd();
    dynamic data = JObject.Parse(jsonData);

    //DB validation's

    var json = "";
    var serializer = new JavaScriptSerializer();
    json = serializer.Serialize(new { success = "0", message = "Test message!" });
    return json;
}

【问题讨论】:

  • 显示您的网络服务代码,并解释“不起作用”的含义。您是否尝试过从浏览器调用 Web 服务?
  • 嘿@nasch,我用Web 服务端更新帖子。不起作用是指函数'LogarAplicativo'无法读取httppost(在应用程序端)传递的信息
  • 我用fiddler在浏览器上测试,json响应正确

标签: c# android json web-services


【解决方案1】:

当您使用UrlEncodedFormEntity 发送信息时,它看起来像the contents of an HTTP form

param1=value1&param2=value2

这不是 JSON 数据,因此您的序列化代码不起作用,因为它是一个完全不同的结构。解析表单数据需要不同的方法,例如HttpUtility.ParseQueryString

【讨论】:

  • 非常感谢 Glorfindel 的光,当我到家时我会测试这个 HttpUtility.ParseQueryString
  • HttpUtility.ParseQueryString 完美运行,现在我意识到我的错误,再次非常感谢
猜你喜欢
  • 2013-12-22
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
  • 2013-03-12
  • 2015-07-02
  • 1970-01-01
相关资源
最近更新 更多