• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with output in screen

alhazza944

Lurker
Mar 7, 2016
2
0
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;

}
}

}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones