In my app i get weather info from internet in asynctask, but sometimes server is a bit laggy, and i want to make up to 10 requests (if previous was unsuccesful) with 10 second waiting between requests. But when i make my asynctask wait 10 sec.(modeling not responding server), main thread(user interface) freezes until asynctask finishes it's job(make 10 rounds of requests).
here is the code where i make and execute asynctask
WeatherGetter wg = new WeatherGetter();
wg.execute(url);
try {
weather = wg.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
and this is where i make it wait
if (cod != 200) {
synchronized (WeatherGetter.this) {
try {
WeatherGetter.this.wait(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Try this way
do not wait the thread
call same function recursively if code!=200 like this
private void loadWhetherData(final int count) {
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
WeatherGetter wg = new WeatherGetter();
wg.execute(url);
try {
weather = wg.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
if (cod != 200 && count<10) {
loadWhetherData(++count);
}
return null;
}
}.execute();
}
Call
This method will calls 10 times until not suceess
loadWhetherData(1);
Related
I've got a problem with my code structure, it works fine but it takes 150-200 ms to process data and it will definitely increase with the size of my database. I was thinking about processing incoming data in a Thread but it could work out. Here is the asynctask:
public class IncomingData extends AsyncTask<Void, Void, String> {
public BufferedReader input;
public String read = null;
public IncomingData() {
}
#Override
public String doInBackground(Void... params) {
try {
input = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
while (input.ready()) {
read = input.readLine();
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
}
return read;
}
#Override
public void onPostExecute(final String input) {
if (input != null) {
InputStreamTokenizer(input);
}
new IncomingData().execute();
}
}
So basicly i'm saying that the function InputStreamTokenizer takes 200+ ms to finish, just after that i can read new incoming lines.
Any idea how could i make a thread or anything of it?
Thanks,
Use executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) instead execute()
I am trying to make a textview change each second by writing a forever-loop in AsyncTask, but the apps doesn't run successfully and I do not know the reason:
private class BackGround extends AsyncTask <Void, Void, Void> {
#Override
protected Void doInBackground(Void...params) {
while(true){
textview.setText("abc");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
textview.setText("def");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
It works fine until the thread finishes the first sleep and tries to reset the textview.
You can't do UI things from doInBackground. You should try calling publishProgress(...) in your while loop to do the UI updates. This will cause onProgressPublished to be called, in which you can actually make the changes.
private class BackGround extends AsyncTask <Void, Void, Void> {
#Override
protected Void doInBackground(Void...params) {
while(true){
publishProgress( "abc" );
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
publishProgress( "def" );
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#Override
protected void onProgressUpdate( String ...progress ){
textView.setText( progress[0] );
}
}
I am sending image using Socket Server and Client. It gived me dialog "App not responding" i think beacuse converting this bitmap was making in UiThread. So i tried to change it but i am still getting this message "App is not responding". It's happening when i am sending big imaes +500kb.
Here is my code for Server:
public class SocketServerThread extends Thread {
static final int SocketServerPORT = 8080;
int count = 0;
#Override
public void run() {
try {
serverSocket = new ServerSocket(SocketServerPORT);
while (true) {
Socket socket = serverSocket.accept();
count++;
// Here where i am doing my code i think is not doing in UiThread..
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run()
{
// Firstly i was doing my code here...
}
});
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
My code for client:
public class MyClientTask extends AsyncTask<Void, Void, Void> {
MyClientTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}
#Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try
{
//I am sending my image here...
}
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
}finally{
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
}
}
So please help my . Why still i am getting not responding dialog?
The ANR error code happens when you block the UI thread more than 5 seconds. If you need to do background work don't use the main thread. Receive the data in a separate thread and post only the result to the UI thread.
I need some help.
I developed an app that parse a web site.
Everything works great now (also thanks to you). But i'm facing a problem when the site that I want to parse is down.
The app just crashes ... I tryed to improve timeout connection, and this works when site is just slow. But how I can manage the server down error?
I would like to print an error in a textview o something like that.
this is part of my code
String result = "";
Document doc = null;
try {
Connection conn = Jsoup.connect(BLOG_URL).timeout(14000);
doc = conn.get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handle the crash when server is down like shown below.
private class doSomethingDelayed extends AsyncTask<Void, Integer, Void> {
private int num_runs = 0;
#Override
protected Void doInBackground(Void... gurk) {
try {
//stuffs...
publishProgress(num_runs);
} catch (ClientProtocolException e) {
} catch (IOException e) {
serviceAvailable = true;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (serviceAvailable == true) {
serviceAvailable = false;
Toast.makeText(getApplicationContext(), "Service not available", Toast.LENGTH_SHORT).show();
System.out.println("in onPostExecute method --");
}
}
#Override
protected void onProgressUpdate(Integer... num_runs) {
try {
} catch (JSONException e) {
e.printStackTrace();
}
}
}
I'm currently working on a tcp client in Android.
I want to connect my android device to a tcp server on my computer and receive the data once every 2 seconds. The problem is that I'm getting force close on my application because of the while loop that I've implemented in the tcp client.
I've tried writing in different ways the loop that will make the tcp client checking the server socket, but with no success. How can make a loop that will check the server socket without getting the force close?
Here's my code that I'm currently using:
public class Connection implements Runnable {
#Override
public void run() {
try {
sk=new Socket(server,port);
viewsurface.setText("connected");
flag = true;
} catch (UnknownHostException e) {
viewsurface.setText("failed 1 socket");
flag = false;
} catch (IOException e) {
viewsurface.setText("failed 2 socket");
flag = false;
}
while (flag == true){
try {
checkin = sk.getInputStream();
checkint = checkin.available();
if (checkint > 0){
try {
BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
received = in.readLine();
viewsurface.setText(received);
} catch (IOException e) {
viewsurface.setText("failed to receive");
}
}
Thread.sleep(2000);
} catch (IOException e) {
viewsurface.setText("checkin failed");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
You need to paste the exception that you are getting to cause the force close, before anyone can provide decent help.
But some suggestions that might solve the problem.
Most likely to be the problem, viewText.setText can only be called from the UI thread. There's quite a few ways to handle this. You can use AsyncTask or if you have an Activity reference you can use runOnUIThread and pass in a runnable that calls setText.
Move checkin = sk.getInputStream(); to before the loop. There's no reason to get the strem every cycle through the loop.
Do not create the BufferedReader every cycle through the loop. Move it before the loop
.sleep(2000) does not guarantee exactly 2 seconds.
I'm having some code formatting issues so I apologize.
private class DownloadFilesTask extends AsyncTask<Void, String, Void> {
protected Long doInBackground(Void... nothing) {
try {
sk=new Socket(server,port);
publishProgress("connected");
flag = true;
} catch (UnknownHostException e) {
publishProgress("failed 1 socket");
flag = false;
} catch (IOException e) {
publishProgress("failed 2 socket");
flag = false;
}
while (flag == true){
try {
checkin = sk.getInputStream();
checkint = checkin.available();
if (checkint > 0){
try {
BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
received = in.readLine();
publishProgress(received);
} catch (IOException e) {
publishProgress("failed to receive");
}
}
Thread.sleep(2000);
} catch (IOException e) {
updateProgress(
} catch (InterruptedException e) {
e.printStackTrace();
}
return;
}
protected void onProgressUpdate(String... progress) {
viewsurface.setText(progress[0]);
}
protected void onPostExecute(Void result) {
//nothing
}
}