1. Download our Official Android App: Forums for Android!

Apps Problem with output in screen

Discussion in 'Android Development' started by alhazza944, Mar 7, 2016.

  1. alhazza944

    alhazza944 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Mar 7, 2016

    Mar 7, 2016
    2
    0
    5
    Male
    Hi all,

    I wrote program to pull XML from apple RSS ( top songs and top albums) . I have two buttons for each. However , I need to press double time to get the result

    This is the code

    package com.example.mohammed.top10downloader;

    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.annotation.MainThread;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.util.Log;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.TextView;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;

    public class MainActivity extends AppCompatActivity {

    private String mFileContent;
    private Button btnParse;
    private Button btnParse1;
    private ListView ListApps;


    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    btnParse = (Button) findViewById(R.id.btnParse);
    btnParse.setOnClickListener(new View.OnClickListener() {
    @override
    public void onClick(View v) {
    ParseApplications parseApplications = new ParseApplications(mFileContent);
    parseApplications.process();
    ArrayAdapter<Application> arrayAdapter = new ArrayAdapter<Application>(MainActivity.this, R.layout.list_item, parseApplications.getApplications());
    ListApps.setAdapter(arrayAdapter);

    DownloadData downloadData = new DownloadData();
    downloadData.execute("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml");
    }

    });
    ListApps = (ListView) findViewById(R.id.xmllistView);



    btnParse1 = (Button) findViewById(R.id.btnParse1);
    btnParse1.setOnClickListener(new View.OnClickListener() {
    @override
    public void onClick(View v) {
    ParseApplications1 parseApplications = new ParseApplications1(mFileContent);
    parseApplications.process();
    ArrayAdapter<Application1> arrayAdapter = new ArrayAdapter<Application1>(MainActivity.this, R.layout.list_item, parseApplications.getApplications());
    ListApps.setAdapter(arrayAdapter);

    DownloadData downloadData1 = new DownloadData();
    downloadData1.execute("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topalbums/limit=10/xml");
    }
    });
    ListApps = (ListView) findViewById(R.id.xmllistView);

    }


    @override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
    }


    @override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
    return true;
    }
    else if (id == R.id.btnParse) {
    DownloadData downloadData = new DownloadData();
    downloadData.execute("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml");
    return true;
    } else if (id == R.id.btnParse1) {
    DownloadData downloadData = new DownloadData();
    downloadData.execute("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topalbums/limit=10/xml");
    return true;
    }
    return super.onOptionsItemSelected(item);
    }


    private class DownloadData extends AsyncTask<String , Void , String >{
    // private String mFileContents;

    @override
    protected String doInBackground(String... params) {
    mFileContent = downlaodXMLFile(params[0]);
    if (mFileContent == null){
    Log.d("DownloadData" , "Error Downloading");
    }
    return mFileContent;
    }

    @override
    protected void onPostExecute(String result) {
    super.onPostExecute(result);
    Log.d("DownloadData", " Result was " + result);

    }

    private String downlaodXMLFile(String urlPath){
    StringBuilder tempBuffer = new StringBuilder();
    try{
    URL url = new URL(urlPath);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    int response = connection.getResponseCode();
    Log.d("DownloadData" , "The response code was " + response);
    InputStream is = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);

    int charRead;
    char [] inputBuffer = new char[500];
    while(true)
    {
    charRead = isr.read(inputBuffer);
    if(charRead <=0)
    {
    break;
    }

    tempBuffer.append(String.copyValueOf(inputBuffer,0,charRead));
    }

    return tempBuffer.toString();

    } catch(IOException e){
    Log.d("DownloadData" , "IO Exception error " + e.getMessage());
    e.printStackTrace();
    } catch(SecurityException e){
    Log.d("DownloadData", "Security Exception: Need Permeation "+ e.getMessage());
    }
    return null;

    }
    }

    }
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England

Share This Page

Loading...