用于将数据发送到服务器的AsyncTask

问题描述 投票:0回答:4

我正在写一个AsyncTask,希望可以在后台将数据连续发送到服务器。我在onCreate()MainActivity中调用了它。但这是行不通的。显示onPreExecute()中的吐司,然后什么也没有发生。

private class MyAsync extends AsyncTask<Void, Void, Void>
 {
      @Override
      protected void onPreExecute() 
      {
          Toast.makeText(getApplicationContext(),"1", Toast.LENGTH_LONG).show();
            // update the UI immediately after the task is executed
            super.onPreExecute();
      }

          @Override
          protected Void doInBackground(Void... params)
          {
             // Toast.makeText(getApplicationContext(),"2", Toast.LENGTH_LONG).show();
              File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt");
              int i=0;

              RandomAccessFile in = null;

              try
              {
                  in = new RandomAccessFile(file, "rw");
              } 
              catch (FileNotFoundException e) 
              {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              //String line =null;
              while (true) 
              {
                HttpEntity entity=null;
                try 
                {
                    if (new GPSLoggerService().isInternetOn()) 
                    {
                            while ((line = in.readLine()) != null)
                            {

                                HttpClient client = new DefaultHttpClient();
                                String url = "http://67.23.166.35:80/android/insert.php";
                                HttpPost request = new HttpPost(url);
                                StringEntity se = new StringEntity(line);
                                se.setContentEncoding("UTF-8");
                                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                                entity = se;
                                request.setEntity(entity);
                                HttpResponse response = client.execute(request);
                                entity = response.getEntity();
                                i++;
                            }
                            if((line = in.readLine()) == null && entity!=null)
                            {
                                file.delete();
                                new MyAsync().execute();
                            }


                    }// end of if
                    else
                    {
                        Thread.sleep(60000);

                    } // end of else
                }// end of try
                catch (NullPointerException e1)
                {
                    e1.printStackTrace();
                } 
                catch (InterruptedException e2) 
                {
                    e2.printStackTrace();
                }
                catch (IOException e1) 
                {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }// end of while

      }//doinbackground

      @Override
      protected void onProgressUpdate(Void... values) {
         // Toast.makeText(getApplicationContext(),"3", Toast.LENGTH_LONG).show();
      }

      @Override
      protected void onPostExecute(Void result) {
          Toast.makeText(getApplicationContext(),"4", Toast.LENGTH_LONG).show();
      }

     }//AsyncTask

请帮助。谢谢。

android android-asynctask
4个回答
3
投票

对于一个问题,您需要对正在发生的事情更加具体。但是,您不想这样做。 AsyncTask用于短时操作/根据Docs


1
投票

您可以使用服务连续上传数据。我提供在后台运行的代码,并在服务器上连续更新数据。


0
投票

我注意到您在asyncTask中的参数全部无效,请尝试将第一个参数更改为String,当然还要在后台将do的参数更改为String,然后将您的类调用到onCreate。以此为参考


0
投票

[验证您的设备具有互联网访问权限,并且已在AndroidManiest.xml文件中授予了互联网访问权限。

© www.soinside.com 2019 - 2024. All rights reserved.