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

这是我在应用程序端编写的代码,正在使用 Xamarin开发</ 代码> </ p>

  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(); \ n requestStream.Write(arrRequest,0,arrRequest.Length);
requestStream.Close();
HttpWebResponse res =(HttpWebResponse)objRequest.GetResponse();
StreamReader streamReader = new StreamReader(res.GetResponseStream()) ;
string result = streamReader.ReadToEnd();
</ code> </ pre>

这是在服务器端编写的代码,即PHP </ p>

< pre> $ recvd = $ _POST ['TH_ET_LoginProcess'];
$ recvdArr = json_decode($ recvd);
</ code> </ pre>

我尝试使用 file_get_contents(“php:// input”)</ code>但这两个方法都返回null或什么都没有。</ p>

使用PHP在Web服务器上读取JSON的最佳方法是什么 情景?</ p>
</ div>

展开原文

原文

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

$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.

What is the best way to read JSON at web server using PHP in such scenario?

1个回答



最佳</ strong>是一个非常主观的术语。</ p>

然而,我是什么 我在Xamarin Android解决方案中成功使用 RestSharp 。</ p>

我也强烈推荐< a href =“https://www.getpostman.com/”rel =“nofollow”> PostMan 和 Fiddler 用于解决您的服务问题。</ p>

关于您的代码:</ strong> </ p>

您应该处置那些对象 是一次性的,最好使用'使用'声明。 这包括HttpWebResponse对象以及Stream的对象。 </ p>
</ div>

展开原文

原文

Best is a quite subjective term.

However what I am successfully using RestSharp in an Xamarin Android solution.

I also highly recommend PostMan and Fiddler for troubleshooting your service.

Regarding your code:

You should be disposing those objects that are disposable, preferable with the 'using' statement. This includes HttpWebResponse object is as well as the Stream ones.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐