Question

I'm working on an app which parses JSON objects from Wikipedia and displays both the 'title' and 'text' attributes. I have looked for similar examples but none work directly with a Wikipedia page.

For example, if I wanted to parse that information from the page:

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

I would have to first select a section to work with, like:

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

and then parse the JSON object, right?. If I wanted to display the title and the text of that specific section, how do I insert the data into two TextViews (one for the title, another for the remaining text)?

EDIT I have the following code:

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;
}
}

But it doesn't work as planned...

Was it helpful?

Solution

1) Parse your JSON object and retrieve the title and the data

2)After declaring your Textview in your activity or fragment set the text of the textviews as following

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

OTHER TIPS

To set a TextView's text, you can simply just call aTextView.setText(someText);

What you can do is count how many sections a certain article has, programatically create x many TextViews (for how many sections the article has) and then set them to what you parse from the JSON.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top