2012-09-28 132 views
0

如何獲取此方法轉發到PHP網頁的數據?使用PHP獲取JSON數據

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); 
    }` 
+0

C#標籤

sb.ToString(); 

這是你從服務器獲取數據? :3請漂亮 – EaterOfCode

回答

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)); 

?>