downloading and parsing a csv file from yahoo finance doesn't work on my android app -
i'm trying make app, shows latest stock quotes, downloading , parsing specific .csv file. doesn't run.. here mainactivity.java:
package com.jdesign.finanzberater; import java.io.bufferedinputstream; import java.io.ioexception; import java.io.inputstream; import java.net.malformedurlexception; import java.net.url; import org.apache.http.util.bytearraybuffer; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class mainactivity extends activity { edittext setsymbol; textview symbolout; textview priceout; textview changepercentageout; button getquote; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); setsymbol = (edittext) findviewbyid(r.id.setsymbol); symbolout = (textview) findviewbyid(r.id.stocksymboloutput); priceout = (textview) findviewbyid(r.id.stockpriceoutput); changepercentageout = (textview) findviewbyid(r.id.stockchangepercentageoutput); getquote = (button) findviewbyid(r.id.get_quote_button); getquote.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { url url; try { url = new url("http://download.finance.yahoo.com/d/quotes.csv?s=" + setsymbol.gettext() + "&f=sl1p2"); inputstream stream = url.openstream(); bufferedinputstream bis = new bufferedinputstream(stream); bytearraybuffer baf = new bytearraybuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } string stocktxt = new string(baf.tobytearray()); string[] tokens = stocktxt.split(","); string stocksymbol = tokens[0]; string stockprice = tokens[1]; string stockchangepercentage = tokens[2]; string fstocksymbol = stocksymbol.substring(1, stocksymbol.length() -1); string fstockchangepercentage = stockchangepercentage.substring(1, stockchangepercentage.length() -3); symbolout.settext(fstocksymbol); priceout.settext(stockprice); changepercentageout.settext(fstockchangepercentage); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }); } } ..my activity_main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <edittext android:id="@+id/setsymbol" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:hint="@string/setsymbol" > <requestfocus /> </edittext> <button android:id="@+id/get_quote_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/get_quote" /> <textview android:id="@+id/stocksymboloutput" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/stockpriceoutput" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/stockchangepercentageoutput" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout> and manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jdesign.finanzberater" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <uses-permission android:name="android.permission.internet"/> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.jdesign.finanzberater.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest> and logcat of course:
08-08 12:26:15.745: d/libegl(24748): loaded /system/lib/egl/libgles_android.so 08-08 12:26:15.755: d/libegl(24748): loaded /system/lib/egl/libegl_adreno200.so 08-08 12:26:15.755: d/libegl(24748): loaded /system/lib/egl/libglesv1_cm_adreno200.so 08-08 12:26:15.755: d/libegl(24748): loaded /system/lib/egl/libglesv2_adreno200.so 08-08 12:26:15.775: d/memalloc(24748): /dev/pmem: mapped buffer base:0x52015000 size:26386432 offset:24297472 fd:62 08-08 12:26:15.785: d/openglrenderer(24748): enabling debug mode 0 08-08 12:26:15.855: d/memalloc(24748): /dev/pmem: mapped buffer base:0x53d20000 size:13746176 offset:11657216 fd:65 08-08 12:26:16.816: d/memalloc(24748): /dev/pmem: mapped buffer base:0x54bc3000 size:4177920 offset:2088960 fd:68 08-08 12:26:17.116: w/textview(24748): getlabel fail! framework orig behavior 08-08 12:26:31.100: d/androidruntime(24748): shutting down vm 08-08 12:26:31.100: w/dalvikvm(24748): threadid=1: thread exiting uncaught exception (group=0x40aac228) 08-08 12:26:31.120: e/androidruntime(24748): fatal exception: main 08-08 12:26:31.120: e/androidruntime(24748): android.os.networkonmainthreadexception 08-08 12:26:31.120: e/androidruntime(24748): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1178) 08-08 12:26:31.120: e/androidruntime(24748):at java.net.inetaddress.lookuphostbyname(inetaddress.java:394) 08-08 12:26:31.120: e/androidruntime(24748): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:245) 08-08 12:26:31.120: e/androidruntime(24748): @ java.net.inetaddress.getallbyname(inetaddress.java:220) 08-08 12:26:31.120: e/androidruntime(24748): @ libcore.net.http.httpconnection.<init>(httpconnection.java:71) 08-08 12:26:31.120: e/androidruntime(24748): @ libcore.net.http.httpconnection.<init>(httpconnection.java:50) 08-08 12:26:31.120: e/androidruntime(24748): @ libcore.net.http.httpconnection$address.connect(httpconnection.java:351) 08-08 12:26:31.120: e/androidruntime(24748): @ libcore.net.http.httpconnectionpool.get(httpconnectionpool.java:86) 08-08 12:26:31.120: e/androidruntime(24748): @ libcore.net.http.httpconnection.connect(httpconnection.java:128) 08-08 12:26:31.120: e/androidruntime(24748): @ libcore.net.http.httpengine.opensocketconnection(httpengine.java:308) 08-08 12:26:31.120: e/androidruntime(24748): @ libcore.net.http.httpengine.connect(httpengine.java:303) 08-08 12:26:31.120: e/androidruntime(24748): @ libcore.net.http.httpengine.sendsocketrequest(httpengine.java:282) 08-08 12:26:31.120: e/androidruntime(24748): @ libcore.net.http.httpengine.sendrequest(httpengine.java:232) 08-08 12:26:31.120: e/androidruntime(24748): @ libcore.net.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:273) 08-08 12:26:31.120: e/androidruntime(24748): @ libcore.net.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:168) 08-08 12:26:31.120: e/androidruntime(24748): @ java.net.url.openstream(url.java:462) 08-08 12:26:31.120: e/androidruntime(24748): @ com.jdesign.finanzberater.mainactivity$1.onclick(mainactivity.java:48) 08-08 12:26:31.120: e/androidruntime(24748): @ android.view.view.performclick(view.java:3549) 08-08 12:26:31.120: e/androidruntime(24748): @ android.view.view$performclick.run(view.java:14393) 08-08 12:26:31.120: e/androidruntime(24748): @ android.os.handler.handlecallback(handler.java:605) 08-08 12:26:31.120: e/androidruntime(24748): @ android.os.handler.dispatchmessage(handler.java:92) 08-08 12:26:31.120: e/androidruntime(24748): @ android.os.looper.loop(looper.java:154) 08-08 12:26:31.120: e/androidruntime(24748): @ android.app.activitythread.main(activitythread.java:4945) 08-08 12:26:31.120: e/androidruntime(24748): @ java.lang.reflect.method.invokenative(native method) 08-08 12:26:31.120: e/androidruntime(24748): @ java.lang.reflect.method.invoke(method.java:511) 08-08 12:26:31.120: e/androidruntime(24748): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 08-08 12:26:31.120: e/androidruntime(24748): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 08-08 12:26:31.120: e/androidruntime(24748): @ dalvik.system.nativestart.main(native method) 08-08 12:26:33.442: d/process(24748): killprocess, pid=24748 08-08 12:26:33.532: d/process(24748): dalvik.system.vmstack.getthreadstacktrace(native method) 08-08 12:26:33.552: d/process(24748): java.lang.thread.getstacktrace(thread.java:599) 08-08 12:26:33.562: d/process(24748): android.os.process.killprocess(process.java:790) 08-08 12:26:33.562: d/process(24748): com.android.internal.os.runtimeinit$uncaughthandler.uncaughtexception(runtimeinit.java:104) 08-08 12:26:33.572: d/process(24748): java.lang.threadgroup.uncaughtexception(threadgroup.java:693) 08-08 12:26:33.602: d/process(24748): java.lang.threadgroup.uncaughtexception(threadgroup.java:690) 08-08 12:26:33.642: d/process(24748): dalvik.system.nativestart.main(native method) 08-08 12:26:33.653: i/process(24748): sending signal. pid: 24748 sig: 9 it nice if correct code.
kind regards,
julien
the following code should work:
inside mainactivity, change listener , add method setresult:
getquote.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { try { list<string> results = new task().execute( setsymbol.gettext().tostring()).get(); setresult(results.get(0), results.get(1), results.get(2)); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (executionexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); private void setresult(string fstocksymbol, string stockprice, string fstockchangepercentage) { symbolout.settext(fstocksymbol); priceout.settext(stockprice); changepercentageout.settext(fstockchangepercentage); } in addition have create asynctask:
class task extends asynctask<string, void, list<string>> { protected list<string> doinbackground(string... symbols) { try { url url; try { url = new url( "http://download.finance.yahoo.com/d/quotes.csv?s=" + symbols[0] + "&f=sl1p2"); inputstream stream = url.openstream(); bufferedinputstream bis = new bufferedinputstream(stream); bytearraybuffer baf = new bytearraybuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } string stocktxt = new string(baf.tobytearray()); string[] tokens = stocktxt.split(","); string stocksymbol = tokens[0]; string stockprice = tokens[1]; string stockchangepercentage = tokens[2]; string fstocksymbol = stocksymbol.substring(1, stocksymbol.length() - 1); string fstockchangepercentage = stockchangepercentage .substring(1, stockchangepercentage.length() - 3); list<string> results = new arraylist<string>(); results.add(fstocksymbol); results.add(stockprice); results.add(fstockchangepercentage); return results; } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return null; } catch (exception e) { e.printstacktrace(); return null; } } } you have add error handling.
Comments
Post a Comment