如下圖所示,本範例使用 OKHttp 來讀取 JSON 多筆資料,並由 GSON(GSON是Google 開發用來解析JSON 格式資料的 Library) 來解析 JSON 格式資料,並由App顯示出相關訊息。
1.找到對應的module ,依照步驟下載 GSON 和 OKHttp 的 Library。
2.OKHttp官網裡有提供GET的範例,可直接套用。
3.lauout.xml。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="gae.melvin.com.app_welcomeview.WelcomeActivity"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/imageView" android:src="@drawable/diary" android:layout_gravity="center_vertical" android:scaleType="fitCenter" /> </LinearLayout>
package gae.melvin.com.app_okhttp_gson; public class Book { private long key; private String title; private String author; private String price; private long time; public long getKey() { return key; } public void setKey(long key) { this.key = key; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public long getTime() { return time; } public void setTime(long time) { this.time = time; } }
5.MainActivity.java。
package gae.melvin.com.app_okhttp_gson; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import com.google.gson.Gson; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import java.io.IOException; public class MainActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView); new RunWrok().start(); } /*上網抓資料,需要另外開執行緒做處理(Android機制)*/ class RunWrok extends Thread { String path_json ="http://android0620-1348.appspot.com/query"; String result_json = null; /* This program downloads a URL and print its contents as a string.*/ OkHttpClient client = new OkHttpClient();String run(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); Response response = client.newCall(request).execute(); return response.body().string(); } Runnable task = new Runnable() { @Override public void run() { //使用 gson 解析 json 資料 Gson gson = new Gson(); Book[] books = gson.fromJson(result_json,Book[].class); StringBuilder sb = new StringBuilder(); for(Book book :books){ sb.append("書名:").append(book.getTitle()).append(" ") .append("作者:").append(book.getAuthor()).append(" ") .append("價錢:").append(book.getPrice()).append("\n\n"); } textView.setText(sb); } }; @Override public void run() { try { //1.抓資料 result_json = run(path_json); //2.改變畫面內容只能用主執行緒(Android機制) runOnUiThread(task); } catch (IOException e) { e.printStackTrace(); } } } }
6.AndroidManifest.xml,上網權限要開。
100分!
回覆刪除