1. Are you ready for the Galaxy S20? Here is everything we know so far!

Problem with output in screen

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

  1. alhazza944

    alhazza944 Lurker
    Thread Starter

    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;

    }
    }

    }
     


Loading...
Similar Threads - Problem output screen
  1. wastar0
    Replies:
    0
    Views:
    136
  2. Daronee
    Replies:
    0
    Views:
    736
  3. Smitherten
    Replies:
    3
    Views:
    242
  4. Eddy155
    Replies:
    3
    Views:
    391
  5. mike WRIGHT
    Replies:
    2
    Views:
    211
  6. AndroidGUIIsComplicated
    Replies:
    0
    Views:
    341
  7. Tim W
    Replies:
    0
    Views:
    247
  8. Kiran123456789
    Replies:
    6
    Views:
    338
  9. Kiran123456789
    Replies:
    0
    Views:
    108
  10. AlienChamp
    Replies:
    23
    Views:
    813

Share This Page

Loading...