c# - 无法从使用 C# 开发的 Android 应用程序接收 json

标签 c# php android json xamarin

这是我在应用端编写的代码,正在使用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 的最佳方法是什么?

最佳答案

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

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

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

关于您的代码:

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

关于c# - 无法从使用 C# 开发的 Android 应用程序接收 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33543624/

相关文章:

php - MySQL 真正的转义字符串

android - ListView 和列表 Activity

c# - 在字符串中找到特定的单词并打印剩余的行

c# - 如何在 C# 中创建未知大小的 byte[]?

c# - 传递接口(interface)而不是对象实例

php - CodeIgniter:项目分页

javascript - 无法在 php 中使用带进度条的 XMLHTTPRequest 提交数据

android - 使用 adb 向 Android 手机发出捏合/捏合命令

android - 根据用户触摸沿其中心旋转 Canvas - Android

c# - 动态 linq 多个与单个 .where 查询