0

如何获取此方法转发到 PHP 网页的数据?

  URL url;
   HttpURLConnection connect = null;
   BufferedReader rd;
   StringBuilder sb;
   OutputStreamWriter wr;
   // Change this url to the url of your receiveJsonSms.php.
   String urlString = "http://www.paintedostrich.com/receiveJsonSms.php";
    try {
  System.setProperty("http.keepAlive", "false");
  url = new URL(urlString);
  connect = (HttpURLConnection) url.openConnection();
  connect.setRequestMethod("POST");
  connect.setDoOutput(true);
  connect.setDoInput(true);
  connect.setReadTimeout(10000);

  connect.connect();

  // write to the stream
  String data = URLEncoder.encode("texts", "UTF-8") + "="
          + URLEncoder.encode(jsonTexts.toString(), "UTF-8");

  wr = new OutputStreamWriter(connect.getOutputStream());
  wr.write(data);
  wr.flush();

  // read the result from the server
  rd = new BufferedReader(new InputStreamReader(connect.getInputStream()));
  sb = new StringBuilder();
  String line = null;
  while ((line = rd.readLine()) != null) {
    sb.append(line);
  }`
4

2 回答 2

0

要在 PHP 中解码 json,请使用json_decode()

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>
于 2012-09-28T10:53:06.710 回答
0

我认为答案是

sb.ToString(); 

如果您想读取 JSON,这就是您从服务器获得的数据,请使用此

http://www.codeproject.com/Tips/397574/Use-Csharp-to-get-JSON-data-from-the-web-and-map-i

于 2012-09-28T10:56:58.110 回答