I want use Java Function in Android Project -
i have function. function worked in java project. want function use android project. while using. project "unfortunately .. has stopped. mi ?
function
public string yandex_translate(string yandex_lang,string yandex_text) throws ioexception, parseexception{ string yandex_key = "****"; string yandex_url = "https://translate.yandex.net/api/v1.5/tr.json/translate?key="+yandex_key+"&lang="+yandex_lang+"&text="+yandex_text; url url = new url(yandex_url); urlconnection httpurlconnection = url.openconnection(); inputstream inputstream = httpurlconnection.getinputstream(); bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(inputstream)); string line=bufferedreader.readline(); jsonparser parser = new jsonparser(); jsonobject jsonobject = (jsonobject) parser.parse(line);; jsonarray msg = (jsonarray)jsonobject.get("text"); if(jsonobject.get("code").tostring().equals("200")){ return msg.get(0).tostring(); }else{ return "Çeviri yapılırken bir hata oluştu."; } }
function using
@override public void onclick(view v) { // todo auto-generated method stub string a; try { = yandex_translate("en-tr","book"); } catch (ioexception e) { // todo auto-generated catch block = e.getmessage(); } catch (parseexception e) { // todo auto-generated catch block = e.getmessage(); } tvkelime.settext(a); }
04-06 19:14:25.128: e/androidruntime(2220): fatal exception: main 04-06 19:14:25.128: e/androidruntime(2220): android.os.networkonmainthreadexception 04-06 19:14:25.128: e/androidruntime(2220): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1117) 04-06 19:14:25.128: e/androidruntime(2220): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385) 04-06 19:14:25.128: e/androidruntime(2220): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) 04-06 19:14:25.128: e/androidruntime(2220): @ java.net.inetaddress.getallbyname(inetaddress.java:214) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpconnection.<init>(httpconnection.java:70) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpconnection.<init>(httpconnection.java:50) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpconnection$address.connect(httpconnection.java:340) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpconnectionpool.get(httpconnectionpool.java:87) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpconnection.connect(httpconnection.java:128) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpengine.opensocketconnection(httpengine.java:316) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpsurlconnectionimpl$httpsengine.makesslconnection(httpsurlconnectionimpl.java:461) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpsurlconnectionimpl$httpsengine.connect(httpsurlconnectionimpl.java:433) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpengine.sendsocketrequest(httpengine.java:290) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpengine.sendrequest(httpengine.java:240) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:282) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:177) 04-06 19:14:25.128: e/androidruntime(2220): @ libcore.net.http.httpsurlconnectionimpl.getinputstream(httpsurlconnectionimpl.java:271) 04-06 19:14:25.128: e/androidruntime(2220): @ com.example.deneme.mainactivity.yandex_translate(mainactivity.java:113) 04-06 19:14:25.128: e/androidruntime(2220): @ com.example.deneme.mainactivity.onclick(mainactivity.java:92) 04-06 19:14:25.128: e/androidruntime(2220): @ android.view.view.performclick(view.java:4204) 04-06 19:14:25.128: e/androidruntime(2220): @ android.view.view$performclick.run(view.java:17355) 04-06 19:14:25.128: e/androidruntime(2220): @ android.os.handler.handlecallback(handler.java:725) 04-06 19:14:25.128: e/androidruntime(2220): @ android.os.handler.dispatchmessage(handler.java:92) 04-06 19:14:25.128: e/androidruntime(2220): @ android.os.looper.loop(looper.java:137) 04-06 19:14:25.128: e/androidruntime(2220): @ android.app.activitythread.main(activitythread.java:5041) 04-06 19:14:25.128: e/androidruntime(2220): @ java.lang.reflect.method.invokenative(native method) 04-06 19:14:25.128: e/androidruntime(2220): @ java.lang.reflect.method.invoke(method.java:511) 04-06 19:14:25.128: e/androidruntime(2220): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 04-06 19:14:25.128: e/androidruntime(2220): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 04-06 19:14:25.128: e/androidruntime(2220): @ dalvik.system.nativestart.main(native method)
you should move code work in different thread (in case asynctask)
asynctask enables proper , easy use of ui thread.
this class allows perform background operations , publish results on ui thread without having manipulate threads and/or handlers.
asynctask designed helper class around thread , handler , not constitute generic threading framework. asynctasks should ideally used short operations (a few seconds @ most.) if need keep threads running long periods of time, highly recommended use various apis provided java.util.concurrent pacakge such executor, threadpoolexecutor , futuretask.
an asynchronous task defined computation runs on background thread , result published on ui thread. asynchronous task defined 3 generic types, called params, progress , result, , 4 steps, called onpreexecute, doinbackground, onprogressupdate , onpostexecute.
it's how write it:
private class asyncrequest extends asynctask<string, void, string> { // private because if calls method without syntax said exception appear again private string yandex_translate(string yandex_lang,string yandex_text) throws ioexception, parseexception { string yandex_key = "****"; string yandex_url = "https://translate.yandex.net/api/v1.5/tr.json/translate?key="+yandex_key+"&lang="+yandex_lang+"&text="+yandex_text; url url = new url(yandex_url); urlconnection httpurlconnection = url.openconnection(); inputstream inputstream = httpurlconnection.getinputstream(); bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(inputstream)); string line=bufferedreader.readline(); jsonparser parser = new jsonparser(); jsonobject jsonobject = (jsonobject) parser.parse(line);; jsonarray msg = (jsonarray)jsonobject.get("text"); if(jsonobject.get("code").tostring().equals("200")){ return msg.get(0).tostring(); }else{ return "Çeviri yapılırken bir hata oluştu."; } } @override protected string doinbackground(string... params) { return yandex_translate(params[0], params[1]); } @override protected void onpostexecute(string s) { super.onpostexecute(s); tvkelime.settext(s); } }
then use it
public void onclick(view v) { // todo auto-generated method stub new asyncrequest("en-tr","book").execute(); }
the edittext
updated when finished.
onpostexecute
, onpreexecute
called in ui thread, can update ui here.
doinbackground
called in worker thread, avoid every ui edit here! if need, use runonuithread
method.
Comments
Post a Comment