简体   繁体   English

无法使用C#开发的Android应用接收json

[英]Not able to receive json from Android app developed using C#

This is the code which I have written on the app side, which is being developed using 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();

This is the code written on the server side which is PHP 这是写在服务器端的PHP代码

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

I tried using file_get_contents("php://input") but both the methods are returning null or nothing. 我尝试使用file_get_contents("php://input")但是两种方法都返回null或什么都不返回。

What is the best way to read JSON at web server using PHP in such scenario? 在这种情况下,使用PHP在Web服务器上读取JSON的最佳方法是什么?

Best is a quite subjective term. 最佳是一个相当主观的术语。

However what I am successfully using RestSharp in an Xamarin Android solution. 但是,我在Xamarin Android解决方案中成功使用RestSharp的方法。

I also highly recommend PostMan and Fiddler for troubleshooting your service. 我也强烈建议PostManFiddler对您的服务进行故障排除。

Regarding your code: 关于您的代码:

You should be disposing those objects that are disposable, preferable with the 'using' statement. 您应该处理那些可抛弃的对象,最好使用“ using”语句。 This includes HttpWebResponse object is as well as the Stream ones. 这包括HttpWebResponse对象以及Stream对象。

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

相关问题 在Android中开发了WP7应用-如何将类型从C#移植到Android? - Having WP7 app developed in Android - how to port types from C# to Android? 在C#和Xamarin开发的移动应用程序上使用Braintree - Using braintree on mobile app developed by C# and Xamarin 无法使用硒C#将值设置为下拉菜单(使用Angular js开发) - Not able to set value to dropdown(developed using angular js) using selenium C# 从用C#开发的COM返回数组 - returning array from a COM developed in C# Android是否通过LAN从C#桌面应用程序接收UDP广播? - Android Receive UDP broadcast from C# desktop app over LAN? 无法使用 c# 从 json 验证基于 json 元素属性值的数据条件 - Not able validate data condition based on the json element attribute value from a json using c# UI 自动化使用 Selenium c# 用于 Windows 应用程序开发使用 Z3055DD731D79EAA7189A62F36724EEF09 应用程序框架 - UI Automation using Selenium c# for Windows App developed with WPF application with CEF framework 使用C#TcpClient接收基于JSON的协议 - Using C# TcpClient to receive a JSON based protocol 如何从Visual Studio中开发的Android C#应用程序访问SoftLayer对象存储。 - How do I access SoftLayer object storage from a C# application for Android, developed in Visual Studio. WWW以json格式从php mysql以C#接收数据 - WWW receive data in C# from php mysql in json format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM