dsafew1231 2015-11-05 11:40
浏览 80

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

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条回答 默认 最新

  • duan97689 2015-11-06 02:28
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型