2014-03-03 72 views
2

我正在研究一個應用程序,它解析來自維基百科的JSON對象並顯示「標題」和「文本」屬性。我找過類似的例子,但沒有一個直接與維基百科頁面一起工作。android:從維基百科文章顯示標題和文本

例如,如果我想解析從頁面信息:

http://en.wikipedia.org/wiki/Plaza_de_España_(Madrid) 

我會先選擇部分一起工作,如:

http://en.wikipedia.org/w/api.php?format=jsonfm&action=parse&page=Plaza_de_España_(Madrid) 

,然後解析JSON對象,對吧?如果我想顯示該特定部分的標題和文本,如何將數據插入到兩個TextView中(一個用於標題,另一個用於剩餘文本)?

編輯 我有以下代碼:

package com.example.jsontest; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
private ProgressDialog pDialog; 

// wikipedia URL 
private static String url = "http://es.wikipedia.org/w/api.php?page=Plaza_de_España_(Madrid)&action=parse&section=6&format=jsonfm"; 
// JSON nose names 
private static final String TAG_TITLE = "title"; 
private static final String TAG_TEXT = "text"; 
private static String TITLE,DATA; 
TextView titleTextView, dataTextView; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    titleTextView = (TextView)findViewById(R.id.titleTextView); 
    dataTextView = (TextView)findViewById(R.id.dataTextView); 
    new GetData().execute(); 
} 

// Async action 
private class GetData extends AsyncTask<Void,Void,Void>{ 
    @Override 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     // show progress dialog 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Espera, por favor..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 
    @Override 
    protected Void doInBackground(Void... arg0){ 
     // create service handler 
     ServiceHandler sh = new ServiceHandler(); 
     // url request 
     String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

     if(jsonStr!=null){ 
      try{ 
       JSONObject jsonObj = new JSONObject(jsonStr); 
       TITLE = jsonObj.getString(TAG_TITLE); 
       Toast.makeText(getBaseContext(), TITLE, Toast.LENGTH_SHORT).show(); 
       DATA = jsonObj.getString(TAG_TEXT); 
       titleTextView.setText(TITLE.toString()); 
       dataTextView.setText(DATA.toString()); 
      }catch(JSONException e){ 
       e.printStackTrace(); 
      } 
     }else{ 
      Toast.makeText(getBaseContext(), R.string.error, Toast.LENGTH_SHORT).show(); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(Void result){ 
     super.onPostExecute(result); 
     // dismiss progress dialog 
     if(pDialog.isShowing()){ 
      pDialog.dismiss(); 
     } 
    } 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
} 

但按計劃它不工作...

+0

你想知道如何改變你的輸入文本視圖文本? –

+0

是的,正是這個 –

回答

1

1)將給您JSON對象並獲取titledata

2)聲明後,你的Textviewactivityfragment設置textviews的文字如下

titleTextView.setText(title); 
dataTextView.setText(data); 
+0

的一節中包含的文本我試過這個,但它不起作用,我要編輯我的問題提供一些aditional代碼 –

+0

我的錯誤是用'format = jsonfm'而不是'format = json'解析URL –

0

要設置TextView的文字,你可以簡單地只是調用aTextView.setText(someText);

你可以做的是統計某篇文章有多少段,以編程方式創建x很多TextView s(關於文章h as),然後將它們設置爲您從JSON解析的內容。

+0

我知道,我想要做的是解析一個URL,從中獲取信息並相應地更改文本。維基百科返回JSON對象,由於每個網頁都會返回自己的格式,因此這些對象並不是微不足道的。我想要做的是解析一個維基百科頁面,並使用它的標題和文章 –

相關問題