我正在研究一個應用程序,它解析來自維基百科的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§ion=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;
}
}
但按計劃它不工作...
你想知道如何改變你的輸入文本視圖文本? –
是的,正是這個 –