AsyncTask and Telnet doesn't show ProgressDialog - android

I am using an asynctask while I am doing some telnet operations. However, the progressdialog is not shown, and I am almost 100% sure that Telnet is the cause.
Please take a look to my code and help me to find where is the problem.
Thanks
public class TelnetManager extends AsyncTask<String, Void, String>{
private TelnetClient telnet;
private int port;
private String IP;
private ProgressDialog dialog;
private Context context;
public TelnetManager(Context c,String IP, int port, String user, String pass)
{
context=c;
this.IP=IP;
this.port=port;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog=new ProgressDialog(context);
dialog.setMessage(context.getResources().getString(R.string.msg_wait));
dialog.show();
}
public String readString() throws IOException
{
InputStream in = new BufferedInputStream(telnet.getInputStream());
int read=0;
String s=null;
do
{
byte[] buffer = new byte[1024];
read = in.read(buffer);
if(read > 0)
{
if(s==null)s=new String(buffer, 0, read);
else s+=new String(buffer, 0, read);
Log.e("S",s);
}
}
while (read > 0);
in.close();
return s;
}
public void writeString(String command) throws IOException
{
OutputStream out = telnet.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out,"UTF-8");
writer.write(command+'\n');
writer.flush();
}
#Override
protected String doInBackground(String... params) {
telnet = new TelnetClient();
String s="";
try {
telnet.setConnectTimeout(10000);
telnet.connect(IP,port);
telnet.setKeepAlive(true);
writeString("password");
writeString(params[0]);
writeString("exit");
String aux=readString();
telnet.getInputStream().close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
telnet.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(dialog!=null && dialog.isShowing())
{
dialog.dismiss();
}
}
}
And here is where I call the AsyncTask:
String list=null;
try {
list=new TelnetManager(this,"192.168.11.30", 10010, null, null).execute("son").get();
construirLayout(list,R.id.containerON);
list=new TelnetManager(this,"192.168.11.30", 10010, null, null).execute("soff").get();
construirLayout(list,R.id.containerOFF);
}
catch (InterruptedException e) {
Toast.makeText(this,"InterruptedException",3000).show();
e.printStackTrace();
}
catch (ExecutionException e) {
Toast.makeText(this,"ExecutionException",3000).show();
e.printStackTrace();
}

If you call get() on an AsyncTask, you're telling the UI thread to block and wait for the AsyncTask results. Since the UI thread is blocked, it cannot show the ProgressDialog.
You should instead provide a callback to the AsyncTask, to be fired in onPostExecute().

Related

Why can't I return a string from an AsyncTask?

I am trying to read an online txt file from my Dropbox and put its content into a string with an Asynctask but I can't manage to return my string.
My Code:
new LongOperation().execute("");
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
String path ="https://dl.dropboxusercontent.com/u/29289946/PCGAMEDONWLOADER/Slots/All%20Games/Rows_available/link2.txt";
URL u = null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer); // Read from Buffer.
bo.write(buffer); // Write Into Buffer.
runOnUiThread(new Runnable() {
#Override
public void run() {
String album = bo.toString();
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return album;
}
#Override
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.loadingtext);
txt.setText("Executed"); // txt.setText(result);
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
}
Now eclipse underlines "album" next to return, so I can't use it in further operations.
Thanks for your help.
Why you run the UI thread to assign the data to a string...you can use it in side your thread.
Simply do in your doInBackground method()like:
bo.write(buffer); // Write Into Buffer.
String album = bo.toString();
return album;
And here your main problem is you define your String variable locally inside runInMainThread() method. Then how you access it out of its surrounding area. If you don't want to change your program then just define the String globally like:
public String doInBackGround()String...params){
String album = "";
.....
....
And asign the value anywhere... like album=bo.toString(); and return ....
You initialized album in this function:
runOnUiThread(new Runnable() {
#Override
public void run() {
String album = bo.toString();
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
So it is only available in this scope. You could have seen this by checking what error Eclipse actually shows you.
It's because you declare your album String in your runOnUiThread, so it will not be known outside of it. Try to declare it in your onPostExecute like follow:
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
String path ="https://dl.dropboxusercontent.com/u/29289946/PCGAMEDONWLOADER/Slots/All%20Games/Rows_available/link2.txt";
URL u = null;
//Declare it here as final
final String album=null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer); // Read from Buffer.
bo.write(buffer); // Write Into Buffer.
runOnUiThread(new Runnable() {
#Override
public void run() {
album = bo.toString();
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return album;
}
#Override
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.loadingtext);
txt.setText("Executed"); // txt.setText(result);
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}

App not responding while transfer data

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.

Running Server not in UiThread

I want to run my ServerSocket and I know how to do it using Thread, but I want to start it not in Ui, so I was thinking about launch this server using AsyncTask.
I have code:
ServerSocket serversocket = new ServerSocket();
serversocket.execute();
public class ServerSocket extends AsyncTask<Void, Void, String>{
public void DataFetcher(android.support.v4.app.FragmentManager fragmentManager){
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(Void... params) {
String data = null;
Toast.makeText(getApplicationContext(), "server is on", Toast.LENGTH_SHORT).show();
return data;
}
#Override
protected void onPostExecute(String result) {
// make gui thread do some work
}
}
but it's not working, can anyone tell me what should I do?
I have ServerClient running using AsyncTask and i want also run my server liek that ,
here is my code for Client:
public class MyClientTask extends AsyncTask<Void, Void, Void> {
String dstAddress="..."
int dstPort=8080;
String response = "";
MyClientTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}
#Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
} 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);
}
}
}
This is wrong
Toast.makeText(getApplicationContext(), "server is on", Toast.LENGTH_SHORT).show();
Toast runs on the main thread and since you can't update UI elements in a background Thread you can't call it from doInBackgroud()
You can do this in onPostExecute() or in onProgressUpdate() by calling publishProgress() in doInBackground()
If it still isn't working then define "but it's not working". That is not a very helpful summary of what is/isn't happening.
I know how to do it using Thread, but I want to start it not in Ui
If you are running it in a separate Thread then it is not running on the UI Thread

how to do an asynchronous task for log in activity?

public static boolean SendMessage(final String response)
{
OutputStream out;
try {
out = socket.getOutputStream();
writeResponse(out,response);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
mRun=true;
return false;
}
return true;
here is my code for sending login message to tcp socket. I want to perform an asynchronous task for this activity. How can i do that.
try code like this and execute new MyAsync().execute(); whenever you want send sms
public class MyAsync extends AsyncTask<Void, Void, Boolean> {
#Override
protected Boolean doInBackground(Void... params) {
return SendMessage(response);
}
}
public static boolean SendMessage(final String response) {
OutputStream out;
try {
out = socket.getOutputStream();
writeResponse(out, response);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
mRun = true;
return false;
}
return true;
}

How to create asyncTask to prevent networkOnMainThreadException

I'm new to android application development. I tried to develop an android server client chat
for my first project. This is the code for the client side. When the client press btnJoin,
it will connect to the server and send a string. I've read many example and many of them
looks like this. I got a networkOnMainThreadException. How do I make an asyncTask to prevent
this problem? Any help would be much appreciated.
btnJoin = (Button) findViewById(R.id.buttonJoin);
btnJoin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket("192.168.1.4", 9092);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF("Hello server!");
txtIP.append(dataInputStream.readUTF() + "\n");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
Change your code as:
btnJoin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view){
new LongOperation().execute("");
}
}
private class LongOperation extends AsyncTask<String, Void, String> {
Socket socket = null;
String strresult="";
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
#Override
protected String doInBackground(String... params) {
try {
socket = new Socket("192.168.1.4", 9092);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF("Hello server!");
strresult.append(dataInputStream.readUTF() + "\n");
// txtIP.append(dataInputStream.readUTF() + "\n");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return strresult;
}
#Override
protected void onPostExecute(String result) {
TextView txtIP= (TextView) findViewById(R.id.txtIP);
// txtIP.append(result + "\n");
txtIP.setText(result + "\n");
}
#Override
protected void onPreExecute() {
}
}
Use AsyncTask like this :
First have it nested in your class, it should look similar to :
private class Communicator extends AsyncTask<Void, Void, Boolean> {
String tmp;
String err;
#Override
protected Boolean doInBackground() {
try {
socket = new Socket("192.168.1.4", 9092);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF("Hello server!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
#Override
protected void onPreExecute() {
}
#Override
protected void onPostExecute(Boolean result) {
txtIP.append(dataInputStream.readUTF() + "\n");
}
}
When you have AsyncTask,you can start it like this :
...
#Override
public void onClick(View v) {
Communicator c=new Communicator();
c.execute();
}
....
try to implement this code in your app
private class LongOperation extends AsyncTask<Object, Integer, Object> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Object doInBackground(Object... params) {
//do hard work here
return params;
}
#Override
protected void onProgressUpdate(Integer... values) {
}
#Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
}
}
AsyncTask must be subclassed to be used. The subclass will override at least one method (doInBackground(Params...)), and most often will override a second one (onPostExecute(Result).)
Here is an example of subclassing:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
Once created, a task is executed very simply:
new DownloadFilesTask().execute(url1, url2, url3);
for more details refer below links...
http://www.vogella.com/articles/AndroidPerformance/article.html
http://developer.android.com/reference/android/os/AsyncTask.html

Categories

Resources