2016-02-13 41 views
0

我對此很新穎。我已經閱讀並觀看並嘗試了很多教程,但爲了理解它的工作原理,我找不到很容易的東西。Android上的一個非常簡單的解析json PHP

我製作了一個php文件,您可以在其中看到表格"result.php"的結果。在表格中,您可以插入First-nameLast-name。這些保存在數據庫中。

"result.php",你可以看到結果爲:

{"nametable":[{"fname":"","lname":""},{"fname":"ni","lname":"gi"}]} 

現在,我想找到一個簡單的方法,以瞭解它,是在我的Android代碼,也將是剛一個文本視圖並顯示名字.. 我不想混淆按鈕,只是一個頁面,它將顯示數據庫中的變量!

有沒有人可以幫助我的代碼,以瞭解它的工作方式?

謝謝!

+0

只是可以肯定,你想顯示textView中的「fname」值? –

回答

0

這是我製作的一個簡單的代碼示例,使用org.json庫& OkHttp客戶端。

它將從服務器中檢索JSON,並在TextView中顯示每個名字。

注意:我沒有測試它,它可能不會編譯,但你明白了。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/textViewWrapper" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- TextView's will be appended here --> 

</LinearLayout> 

MyActivity.java

//...other imports 

import org.json.*; 
import okhttp3.*; 

public class MyActivity extends AppCompatActivity { 

    private LinearLayout textViewWrapper; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //Get TextView Wrapper 
     textViewWrapper = (LinearLayout) findViewById(R.id.textViewWrapper); 

     fillTextView(); 
    } 

    /** 
    * Fill TextViews with API response. 
    * 
    */ 

    public void fillTextView() { 

     get("http://example.com/result.php", new Callback() { 
     @Override 
     public void onFailure(Call call, IOException e) {} 

     @Override 
     public void onResponse(Call call, final Response response) throws IOException { 

      final String body = response.body().string(); //Get API response 

      //OKHttp callback isn't in the main thread, 
      //UI operations need to be run in the main thread 
      //That's why you should use runOnUiThread 
      MyActivity.this.runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 

        JSONObject JSON = null; 
        try { 
        //Parse JSON 
        JSON = new JSONObject(body); 
        //Get "nametable" array 
        JSONArray nameTable = JSON.getJSONArray("nametable"); 
        //Loop through "nametable" array 
        for (int i = 0; i < nameTable.length(); i++) { 

         JSONObject item = nameTable.getJSONObject(i); 

         //Create TextView 
         TextView rowTextView = new TextView(MyActivity.this); 

         //Set fname 
         rowTextView.setText(item.getString("fname")); 

         // add the textview 
         textViewWrapper.addView(rowTextView); 
        } 

        } catch (JSONException e) { 
        e.printStackTrace(); 
        } 
       } 
      }); 
     } 
     }); 

    } 

    /** 
    * OKHttp GET helper function (You should probably put this in a Request Utils class) 
    * 
    */ 

    public Call get(String url, Callback callback) { 

     OkHttpClient client = new OkHttpClient(); 

     okhttp3.Request request = new okhttp3.Request.Builder() 
     .url(url) 
     .get() 
     .build(); 

     Call call = client.newCall(request); 
     call.enqueue(callback); 

     return call; 

    } 
} 

在這個問題解析JSON的更多信息:How to parse JSON in Java

+0

感謝您的回覆,併爲我的延遲感到抱歉! 我想問你一些細節,以嘗試和運行它! 首先,我有我的第一個錯誤「import okhttp3。*;」 它說,不能解析符號... 接下來,缺少新的Callback()庫。問題是,有太多的選擇,以選擇導入alt +進入,我不知道女巫是適當的..你能幫助我嗎? 我認爲當「新回調()」下一些問題的一些會消失.. 你可以幫我,以繼續我的代碼?謝謝!! :) –

+0

您需要安裝庫。將依賴關係添加到您的gradle。 'compile'c​​om.squareup.okhttp3:okhttp:3.1.2''檢查我提供的鏈接okhttp。 –