My code doesn't start new threads or anything. Hmm... ive no idea what is going on then. I thought if it runs fine on the emulator that it is 100% gonna work on a phone. :/
Code:
package com.my.app;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class CurrencyActivity extends Activity {
private Button convertBtn;
private Double SEKrate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.curr_layout);
convertBtn=(Button)findViewById(R.id.convertBtn);
convertBtn.setOnClickListener(convertBtnClick);
if(checkInternetConnection()==true)
{
LoadDataWeb();
}
else if(checkInternetConnection()==false)
{
//read the offline file
System.out.println("No internet connection.");
String Path = Environment.getExternalStorageDirectory().getPath() + "/CURR/curr.xml";
File f = new File(Path);
if(f.exists())
{
LoadData();
}
else
{
Intent intent = new Intent(getApplicationContext(), NoInternet.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
}
public Double getSEKrate() {
return SEKrate;
}
public void setSEKrate(Double sEKrate) {
SEKrate = sEKrate;
}
private OnClickListener convertBtnClick=new OnClickListener(){
public void onClick(View v) {
convertCurrency();
}
};
public boolean checkInternetConnection() {
ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) {
return true;
}
else {
return false;
}
}
public void LoadDataWeb() {
//update file and read
System.out.println("We have internet connection.");
HttpURLConnection connection = null;
BufferedReader rd = null;
StringBuilder sb = null;
String line = null;
URL serverAddress = null;
try {
serverAddress = new URL("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
//set up out communications stuff
connection = null;
//Set up the initial connection
connection = (HttpURLConnection)serverAddress.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setReadTimeout(10000);
connection.connect();
//read the result from the server
rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
sb = new StringBuilder();
while ((line = rd.readLine()) != null)
{
sb.append(line + '\n');
}
System.out.println(sb.toString());
String state = Environment.getExternalStorageState();
System.out.println(state);
if(state.equals("mounted"))
{
String Path = Environment.getExternalStorageDirectory().getPath() + "/CURR/curr.xml";
File f = new File(Path);
System.out.println(Path);
f.createNewFile();
FileOutputStream fOut = new FileOutputStream(f);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(sb.toString());
myOutWriter.close();
fOut.close();
System.out.println("File written to " + Path);
if(f.exists())
{
LoadData();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
//close the connection, set all objects to null
connection.disconnect();
rd = null;
sb = null;
connection = null;
}
}
public void LoadData() {
try {
String Path = Environment.getExternalStorageDirectory().getPath() + "/CURR/curr.xml";
File fXmlFile = new File(Path);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("Cube");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
if(temp==1)
{
String date = eElement.getAttribute("time");
System.out.println("DATE : " + date);
}
if(temp==13)
{
String curr = eElement.getAttribute("currency");
System.out.println("CURRENCY:" + curr);
String rate = eElement.getAttribute("rate");
System.out.println("RATE:" + rate);
Double SEKrate = Double.parseDouble(rate);
this.setSEKrate(SEKrate);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void convertCurrency() {
EditText editText = (EditText)findViewById(R.id.amountBox);
String editTextStr = editText.getText().toString();
Double inputNum = Double.parseDouble(editTextStr);
Double result = inputNum*SEKrate;
TextView text = (TextView) findViewById(R.id.result);
text.setText(String.format("%.2f", result));
}
}
I hate bombarding you with all my code but in your free time could you take a look?
Bookmarks