Android FTP Server

前端 未结 5 503
抹茶落季
抹茶落季 2020-12-28 23:43

I am using the following code to make the android device a ftp server (Android Internal storage). I am getting the exception of os.android.NetworkOnMainThread.

相关标签:
5条回答
  • 2020-12-29 00:06

    while(true){ incoming = s.accept(); ...} You cannot put that in OnStart(). That should be done in a thread. So ServerSocket s = null; should be a variable of you activity.

    0 讨论(0)
  • 2020-12-29 00:08

    You can not do network operation in main thread in android 3.0 higher. Use AsyncTask for this network operation. See this for further explanation

    0 讨论(0)
  • 2020-12-29 00:17

    Maybe because you didn't set up the permissions in the manifest? You've to set permission for internet usage.

    If this doesn't work, please tell us which line is it throwing the exception.

    0 讨论(0)
  • 2020-12-29 00:19

    Using the Swiftp application (open source) as a service in my application helped me to acheive my task. Thanks for everyones help. Here is the link if someone wants to follow

    0 讨论(0)
  • 2020-12-29 00:20

    Please post your code here.

    NetworkOnMainthreadException occurs because you maybe running Network related operation on the Main UI Thread. You should use asynctask for this purpose

    This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

    http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

    class TheTask extends AsyncTask<Void,Void,Void>
    {
    protected void onPreExecute()
      {           super.onPreExecute();
                 //display progressdialog.
      }  
    
    protected void doInBackground(Void ...params)//return result here
    {  
    //http request. do not update ui here
    //call webservice
    //return result here
    return null;
     } 
    
    protected void onPostExecute(Void result)//result of doInBackground is passed a parameter
    {     
        super.onPostExecute(result);
        //dismiss progressdialog.
        //update ui using the result returned form doInbackground()
    } 
    }
    

    http://developer.android.com/reference/android/os/AsyncTask.html. Check the topic under the heading The 4 Steps.

    A working example of asynctask @ To use the tutorial in android 4.0.3 if had to work with AsynxTasc but i still dont work?.

    The above makes a webserive call in doInBakckground(). Returns result and updates the ui by setting the result in textview in onPostExecute().

    0 讨论(0)
提交回复
热议问题