Android FTP Server

前端 未结 5 522
抹茶落季
抹茶落季 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: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
    {
    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().

提交回复
热议问题