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
助けてください。ありがとうございました。