2015-10-29 26 views
0

我有以下的php文件可以在網絡主機上正常工作。如何通過JSON將PHP的輸出轉換爲分析的數據

<?php 
    $dbhost = 'dbhost'; 
    $dbuser = 'dbuser'; 
    $dbpass = 'pass'; 

    $conn = mysql_connect($dbhost, $dbuser, $dbpass); 

    if(! $conn) 
    { 
     die('Could not connect'.mysql_error()); 
    } 

    $sql = "SELECT FIELD1, FIELD2, FIELD3, FIELD4 FROM `TABLE` WHERE FIELD1 =  'VALUE1' "; 
    mysql_select_db('MyDB'); 
    $retval = mysql_query($sql, $conn); 

    if(! $retval) 
    { 
     die('Could not get data: ' . mysql_error()); 
    } 

    while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) 
    { 
     echo "FIELD1" :{$row['FIELD1']} <br> ". 
     "FIELD2 : {$row['FIELD2']} <br> ". 
     "FIELD3 : {$row['FIELD3']} <br> "; 
     $output[] = $row; 
    } 

    print(json_encode($row)); 
    mysql_close($conn); 
?> 

Link是這裏http://marcodr.byethost7.com/TP2.php

現在,我想顯示在一個TextView這個內容,我unseccessfully使用下面的代碼

JSONObject object=getJSONObject(「result」); //comes from the  print(json_encode ($result)); 
JSONArray arr=object.getJSONArray(「Value1」); //transforms the object Value1 in array 
for(int i=0;i<arr.length;i++){ 
JSONObject obj1=arr.getJSONObject(i); 
String Value1=obj1.getString("Value1"); 
TextView.setText(Value1); //set TextView’s content to the String Value1 
} 

清單有權瀏覽互聯網,文本視圖是在佈局等,但仍然不能把PHP輸出到TextView中。我的代碼有什麼問題?

+0

因爲你不輸出json。您正在輸出調試文本** AND ** json,並且由於調試文本不是有效的json,所以整個文本blob被json解析器視爲無效。 –

+0

Hi @MarcB,感謝您的反饋。任何進一步的提示?或者您是否知道我可以找到解決方案的鏈接? – MDR

回答

0

在PHP端:

$obj = new stdClass(); 
$obj->label="Price of an item"; 
$obj->data = array(
    array('1999','200'), 
    array('2000','210'), 
    array('2007','240') 
); 

echo json_encode($obj); 

我們假設你的JSON是這樣的:

[ 
    { 
     "response": [ 
      { 
       "1999": "200", 
       "2000": "210", 
       "2007": "240" 
      } 
     ] 
    } 
] 

在Android中端:

String jsonString3 = 
       "[\n" + 
         " {\n" + 
         "  \"response\": [\n" + 
         "   {\n" + 
         "    \"1999\": \"200\",\n" + 
         "    \"2000\": \"210\",\n" + 
         "    \"2007\": \"240\"\n" + 
         "   }\n" + 
         "  ]\n" + 
         " }\n" + 
         "]"; 
     try{ 
      JSONArray array = new JSONArray(jsonString3); 
      JSONArray priceArray = array.getJSONObject(0).getJSONArray("response"); 
      Log.d(LOG_TAG,"Price Array = " + priceArray.toString()); 

      for (int i = 0; i < priceArray.length(); i++){ 
       Log.d(LOG_TAG, "1999 = " + priceArray.getJSONObject(i).getString("1999")); 
       Log.d(LOG_TAG, "2000 = " + priceArray.getJSONObject(i).getString("2000")); 
       Log.d(LOG_TAG, "2007 = " + priceArray.getJSONObject(i).getString("2007")); 
      } 

     }catch (JSONException ex){ 
      Log.e(LOG_TAG, ex.toString()); 
     } 

假設你從你的PHP API收到jsonData。我希望這可以幫助你:

boolean success = false; 
     int temp= 0; 
     try { 
      JSONObject jsonobject = new JSONObject(jsonData); 
      success = jsonobject.getBoolean("success"); 
      if (success) { 
       // Locate the NodeList name 
       JSONArray jsonarray = jsonobject.getJSONArray("data"); 
       for (int i = 0; i < jsonarray.length(); i++) { 
        temp = i+1; 
        jsonobject = jsonarray.getJSONObject(i); 
        String id = jsonobject.getString("FIELD"+temp); 
        String values = jsonobject.getString("value"); 
        Log.e(LOG_TAG, "FIElD"+temp+": "+id); 
        Log.e(LOG_TAG, "Value: "+values); 
       } 
      } 
     } catch (JSONException e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
+0

哇@穆罕默德!我可能有一個更好的PHP文件http://marcodr.byethost7.com/fahmi.php 事情是,而不是一個數組,我需要給定產品的FIELD1,FIELD2和FIELD3中的信息 – MDR

+0

我剛剛格式化了JSON。但是我有一個問題:如果我不知道什麼是PHP輸出,我怎麼才能輸入jsonString3內容? 在我的情況下,我想創建一個應用程序,將數據從MySQL轉移到Android界面,每天更改數據。任何想法? – MDR

+0

我在回答中添加了一個部分,我希望它有幫助。 – Mohammad

0

試試這個:

String jsonString3 = 
       "[\n" + 
         " {\n" + 
         "  \"response\": \"\",\n" + 
         "  \"FIELD1\": [\n" + 
         "   {\n" + 
         "    \"value\": \"A\"\n" + 
         "   }\n" + 
         "  ],\n" + 
         "  \"FIELD2\": [\n" + 
         "   {\n" + 
         "    \"value\": \"B\"\n" + 
         "   }\n" + 
         "  ],\n" + 
         "  \"FIELD3\": [\n" + 
         "   {\n" + 
         "    \"value\": \"C\"\n" + 
         "   }\n" + 
         "  ],\n" + 
         "  \"FIELD4\": [\n" + 
         "   {\n" + 
         "    \"value\": \"D\"\n" + 
         "   }\n" + 
         "  ]\n" + 
         " }\n" + 
         "]"; 
     try{ 

      JSONArray data = new JSONArray(jsonString3); 
      JSONObject response = data.getJSONObject(0); 
      Iterator<?> keys = response.keys(); 
      while(keys.hasNext()){ 
       String key = (String)keys.next(); 
       if (response.get(key) instanceof JSONArray) 
       { 
        JSONArray aArray = (JSONArray) response.getJSONArray(key); 
        for (int i = 0; i < aArray.length(); i++){ 
         Log.d(LOG_TAG, "item = " + key); 
         Log.d(LOG_TAG, "it's value = " + aArray.getJSONObject(i).getString("value")); 
        } 
       } 

      } 

     }catch (JSONException ex){ 
      Log.e(LOG_TAG, ex.toString()); 
     } 

但是,我的JSON是硬編碼。如果你從PHP api收到你的JSON,你應該總結一下這樣的解決方案(我從我的api得到了我的jsonData):

String jsonData = apiCaller(params); 
    boolean success = false; 
      int temp= 0; 
      try { 
       JSONObject jsonobject = new JSONObject(jsonData); 
       success = jsonobject.getBoolean("success"); 
       if (success) { 
        // Locate the NodeList name 
        JSONArray jsonarray = jsonobject.getJSONArray("data"); 
        for (int i = 0; i < jsonarray.length(); i++) { 
         temp = i+1; 
         jsonobject = jsonarray.getJSONObject(i); 
         String id = jsonobject.getString("FIELD"+temp); 
         String values = jsonobject.getString("value"); 
         Log.e(LOG_TAG, "FIElD"+temp+": "+id); 
         Log.e(LOG_TAG, "Value: "+values); 
        } 
       } 
      } catch (JSONException e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
相關問題