【问题标题】:Not able to receive json from Android app developed using C#无法从使用 C# 开发的 Android 应用程序接收 json
【发布时间】:2016-02-06 05:34:50
【问题描述】:

这是我在app端写的代码,正在使用Xamarin开发

HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(ApiUrl);
objRequest.Method = "POST";
        objRequest.ContentType = "application/x-www-form-urlencoded";//"application/json;charset=utf-8";
        objRequest.Headers.Add("Authentication", "web60134");       
        UTF8Encoding utf8Encoding = new UTF8Encoding();
        byte[] arrRequest;
        arrRequest = utf8Encoding.GetBytes(JsonReq);
        objRequest.ContentLength = arrRequest.Length;
        Stream requestStream = objRequest.GetRequestStream();
        requestStream.Write(arrRequest, 0, arrRequest.Length);
        requestStream.Close();
        HttpWebResponse res = (HttpWebResponse)objRequest.GetResponse();
        StreamReader streamReader = new StreamReader(res.GetResponseStream());
        string result = streamReader.ReadToEnd();

这是在服务器端编写的代码,即 PHP

$recvd = $_POST['TH_ET_LoginProcess'];
$recvdArr = json_decode($recvd);

我尝试使用 file_get_contents("php://input"),但两种方法都返回 null 或什么都不返回。

在这种情况下,使用 PHP 在 Web 服务器上读取 JSON 的最佳方法是什么?

【问题讨论】:

    标签: c# php android json xamarin


    【解决方案1】:

    最佳是一个相当主观的术语。

    但是,我在 Xamarin Android 解决方案中成功使用了 RestSharp

    我还强烈推荐 PostManFiddler 对您的服务进行故障排除。

    关于您的代码:

    您应该处理那些一次性的对象,最好使用“使用”语句。这包括 HttpWebResponse 对象以及 Stream 对象。

    【讨论】:

      猜你喜欢
      • 2013-09-03
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 2016-02-01
      相关资源
      最近更新 更多