2016-09-28 17 views
0

此外,當我更改屏幕方向時,數據會更新。 當我敬酒的數據,那麼它的5秒後更改,但不爲什麼數據沒有得到updated.And onPreExecute()每5秒AsyncTask(update)僅在第一次打開應用程序時起作用,但5秒後不會更新。

1.Here後也在努力是我的update.java文件

public class update extends Activity implements LocationListener { 
    protected String mLatitudeText; 
    protected String mLongitudeText; 
    protected String mCode = "1001"; 
    protected java.net.URL url; 
    protected HttpURLConnection conn; 
    android.os.Handler customHandler; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 0); 
     return; 
    } 
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    lm.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 5, 0, (android.location.LocationListener) this); 
    Location mLastLocation = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 

    mLatitudeText = (String.valueOf(mLastLocation.getLatitude())); 
    mLongitudeText = (String.valueOf(mLastLocation.getLongitude())); 

    final android.os.Handler handler = new android.os.Handler(); 
    Timer timer = new Timer(); 
    TimerTask backtask = new TimerTask() { 
     @Override 
     public void run() { 
      handler.post(new Runnable() { 
       public void run() { 
        try { 
         LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
         Location mLastLocation = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 

         mLatitudeText = (String.valueOf(mLastLocation.getLatitude())); 
         mLongitudeText = (String.valueOf(mLastLocation.getLongitude())); 
         String[] s = new String[3]; 
         s[0]=mCode; 
         s[1]=mLatitudeText; 
         s[2]=mLongitudeText; 
         Toast.makeText(getApplicationContext(), mLatitudeText + " and " + mLongitudeText, Toast.LENGTH_LONG).show(); 
         makePOST.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,s); 

        } catch (Exception e) { 
         // TODO Auto-generated catch block 
        } 
       } 
      }); 
     } 
    }; 
    timer.schedule(backtask, 0, 5000); 
} 
    AsyncTask<String, Void, String> makePOST=new AsyncTask<String, Void, String>(){ 
    protected void onPreExecute() { 
     Toast.makeText(update.this, "onPreExecute", Toast.LENGTH_LONG).show(); 
    } 
    protected String doInBackground(String... params) { 
      try { 
       mCode=params[0]; 
       mLongitudeText=params[1]; 
       mLongitudeText=params[2]; 
       url = new URL("http://student.96.lt/Android/update.php"); 
       conn = (HttpURLConnection) url.openConnection(); 
       conn.setDoOutput(true); 
       conn.setDoInput(true); 
       conn.setRequestMethod("POST"); 
       String postdat = "code" + "=" + URLEncoder.encode(mCode, "UTF-8") + "&" + "latitude" + "=" + URLEncoder.encode(mLatitudeText, "UTF-8") + "&" + "longitude" + "=" + URLEncoder.encode(mLongitudeText, "UTF-8"); 
       //String postdat=mLatitudeText; 
       conn.setFixedLengthStreamingMode(postdat.getBytes().length); 
       conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       OutputStream OS = conn.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8")); 
       bufferedWriter.write(postdat); 
       bufferedWriter.flush(); 
       // Get output 
       bufferedWriter.close(); 
      } catch (java.net.MalformedURLException ex) { 
       //Toast.makeText(this, ex.toString(), duration).show(); 
      } catch (java.io.IOException ex) { 
       //Toast.makeText(this, ex.toString(), duration).show(); 
      } 
      return null; 
     } 

    }; 
@Override 
    public void onLocationChanged (Location location){ 



    } 

    @Override 
    public void onProviderDisabled (String provider){ 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled (String provider){ 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged (String provider,int status, Bundle extras){ 
     // TODO Auto-generated method stub 

    } 
} 

請幫助我,我處於初級階段。

+0

因此,您無法更新最新的位置緯度和長到您的服務器因爲'doInBackground'無法正常工作? – San

+0

請告訴我該怎麼做? –

+0

我想了解你的問題..你無法實現的是什麼? – San

回答

1

任務只能運行一次。要再次運行相同的代碼,您需要創建一個新的AsyncTask對象。它拋出一個異常告訴你這一點,但你忽略了所有的異常(通常是一個壞主意)。

+0

那麼我該怎麼做?請幫我 –

+0

創建一個新的AsyncTask。現在您正在創建1並將其存儲在值makePost中。相反,每次都要分配一個新的。 –

+0

你能告訴我該怎麼做嗎?如果你輸入一些代碼行可能會更好 –

相關問題