綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 請問要如何讀取網頁JSON格式呢?

[複製連結] 查看: 3561|回覆: 6|好評: 0
跳轉到指定樓層
樓主
wind8894 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2012-12-10 19:18

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
我有一個網頁只顯示JSON格式內容
  1. {"code":"84f9df8db8f2210c45e0f7792f061c2e","state":""}
複製代碼
想寫一隻JSON程式去讀取這串

要如何做呢?


「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏1 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
ploglin | 收聽TA | 只看該作者
發表於 2012-12-11 09:14
大致如下~

  1. String json = "{"code":"84f9df8db8f2210c45e0f7792f061c2e","state":""}";
  2. JSONObject object = new JSONObject(json);
  3. String code = object.getString("code");
複製代碼
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

板凳
 樓主| wind8894 | 收聽TA | 只看該作者
發表於 2012-12-11 16:46
有辦法做String url=" "嗎?

因為"84f9df8db8f2210c45e0f7792f061c2e"會浮動改變
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

地板
ploglin | 收聽TA | 只看該作者
發表於 2012-12-11 17:47
不太懂你的意思耶?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

5
 樓主| wind8894 | 收聽TA | 只看該作者
發表於 2012-12-11 17:49
比如說網頁上面顯示
  1. {"code":"84f9df8db8f2210c45e0f7792f061c2e","state":""}
複製代碼
然後我要去撈

如果這樣寫
  1. String json = "{"code":"84f9df8db8f2210c45e0f7792f061c2e","state":""}";
  2. JSONObject object = new JSONObject(json);
  3. String code = object.getString("code");
複製代碼
不就是寫死了嗎?

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

6
ploglin | 收聽TA | 只看該作者
發表於 2012-12-12 10:01
一般來說 key 通常都是固定的,同一個功能回傳的內容如果 key 變來變去,那就很難處理了。
api 是你自己寫的嗎?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

7
 樓主| wind8894 | 收聽TA | 只看該作者
發表於 2012-12-12 16:49
經過高人指點後,我用出來了,分享一下程式碼
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.client.HttpClient;
  5. import org.apache.http.client.methods.HttpGet;
  6. import org.apache.http.impl.client.DefaultHttpClient;
  7. import org.json.JSONObject;
  8. import android.app.Activity;
  9. import android.os.Bundle;
  10. import android.widget.TextView;

  11. public class Json extends Activity {
  12.     private TextView get_code,get_state;
  13.     @Override
  14.     public void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.main);
  17.         get_code=(TextView)findViewById(R.id.get_code);
  18.         get_state=(TextView)findViewById(R.id.get_state);
  19.         
  20.         HttpClient client = new DefaultHttpClient();
  21.         StringBuilder builder = new StringBuilder();
  22.         HttpGet get =
  23.         new HttpGet("要抓取資料的網址");        
  24.         try {
  25.             HttpResponse response = client.execute(get);
  26.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  27.                     response.getEntity().getContent()));
  28.             for (String s = reader.readLine(); s != null; s = reader.readLine()) {
  29.                 builder.append(s);
  30.             }
  31.             JSONObject jsonObject = new JSONObject(builder.toString());            
  32.             String myCode =jsonObject.getString("code");
  33.             String myState = jsonObject.getString("state");
  34.             get_code.setText(myCode);
  35.                         get_state.setText(myState);
  36.         }
  37.         catch (Exception e)
  38.         {
  39.             e.printStackTrace();
  40.         }
  41.     }
  42. }
複製代碼

評分

參與人數 2幫助 +2 收起 理由
willfall + 1
ploglin + 1

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則