Android FTP Server -
i using following code make android device ftp server (android internal storage). getting exception of os.android.networkonmainthread
. have tried put onstart code in asynctask
app never executes , crashes on launch. regarding ftp server on android great have no idea how working.
here mainactivity
code
package com.googlecode.simpleftp; import java.io.ioexception; import java.net.serversocket; import java.net.socket; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import android.app.activity; import android.app.alertdialog; import android.app.dialog; import android.content.context; import android.content.dialoginterface; import android.os.bundle; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.widget.textview; import android.widget.toast; public class ftpserver extends activity { private static int command_port = 2121; static final int dialog_alert_id = 0; private static executorservice executor = executors.newcachedthreadpool(); /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.my_menu, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle item selection switch (item.getitemid()) { case r.id.new_game: system.out.println("new game button pressed!"); //newgame(); return true; case r.id.quit: system.out.println("quit button pressed!"); showdialog(dialog_alert_id); return true; default: return super.onoptionsitemselected(item); } } @override protected dialog oncreatedialog(int id){ alertdialog.builder builder = new alertdialog.builder(this); builder.setmessage("are sure want exit?") .setcancelable(false).setpositivebutton("yes", new dialoginterface.onclicklistener(){ @override public void onclick(dialoginterface dialog, int id){ ftpserver.this.finish(); } }) .setnegativebutton("no", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); alertdialog alert = builder.create(); return alert; }
here serverpi code
import java.io.bufferedreader; import java.io.file; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.socket; public class serverpi implements runnable{ private socket clientsocket; private bufferedreader in; private printwriter out; private string basedir; private string relativedir; private string absolutedir; private string filename; private string filepath; public serverpi(socket incoming) throws ioexception{ this.clientsocket = incoming; in = new bufferedreader(new inputstreamreader(this.clientsocket.getinputstream())); out = new printwriter(this.clientsocket.getoutputstream(), true); basedir = new file("").getabsolutepath(); relativedir = "/"; absolutedir = basedir + relativedir; filename = ""; filepath = absolutedir + "/" + filename; } private void readcommandloop() throws ioexception { string line = null; reply(220, "welcome simpleftp server!"); while((line = in.readline()) != null){ int replycode = executecommand(line.trim()); if(replycode == 221){ return; } } } private int executecommand(string trim) { // todo auto-generated method stub return 0; } public int reply(int statuscode, string statusmessage){ out.println(statuscode + " " + statusmessage); return statuscode; } @override public void run(){ try{ this.readcommandloop(); } catch (ioexception e){ e.printstacktrace(); } { try { if(in != null){ in.close(); in = null; } if(out != null){ out.close(); out = null; } if (clientsocket != null){ clientsocket.close(); clientsocket = null; } } catch (ioexception e){ e.printstacktrace(); } } } }
i have put code in asynctask, here is
private class longoperation extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { serversocket s = null; socket incoming = null; try{ s = new serversocket(command_port); string ip = (s.getinetaddress()).gethostaddress(); context context = this.getapplicationcontext(); charsequence text = ip; int duration = toast.length_long; toast toast = toast.maketext(context, text, duration); thread.sleep(1000); toast.show(); while(true){ incoming = s.accept(); executor.execute(new serverpi(incoming)); } } catch(exception e){ system.out.println(e.tostring()); e.printstacktrace(); } finally{ try { if(incoming != null)incoming.close(); } catch(ioexception ignore) { //ignore } try { if (s!= null) { s.close(); } } catch(ioexception ignore) { //ignore } } return "executed"; } @override protected void onpostexecute(string result) { } @override protected void onpreexecute() { } @override protected void onprogressupdate(void... values) { } }
iam calling longopertation in oncreate method. problem app crashes on launch.
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_fullscreen); new longoperation().execute(); }
maybe because didn't set permissions in manifest? you've set permission internet usage.
if doesn't work, please tell line throwing exception.
Comments
Post a Comment