public class SocketThread {
private boolean connect = false;
private String ip = "*********";
private int port = ****;
private Socket socket;
private SocketAsync socketAsync;
public SocketThread() {
socketAsync = new SocketAsync();
socketAsync.execute();
}
public void setMessenger(SocketServiceMessenger messenger) {
this.socketServiceMessenger = messenger;
}
public void setConnectFlag(boolean connect) {
this.connect = connect;
}
public void sentData(JSONObject json) {
socketAsync.sentData2(json);
}
private class SocketAsync extends AsyncTask<Void, Void, String> {
private PrintWriter printWriter;
#Override
protected String doInBackground(Void... params) {
String msgStr;
String type;
try {
socket = new Socket(InetAddress.getByName(ip),port);
OutputStreamWriter streamOut = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
printWriter = new PrintWriter(streamOut);
streamOut.flush();
BufferedReader streamIn = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
while(socket.isConnected()) {
msgStr = streamIn.readLine();
System.out.println(msgStr);
if(!connect) {
socket.close();
}
}
System.out.println("SocketDisconnected");
} catch (Exception e) {
System.out.println(e.toString());
}
return null;
}
public void sentData2(JSONObject json) {
if(socket.isConnected()) {
printWriter.println(json.toString());
printWriter.flush();
System.out.println("OUT : " + json);
}
}
}
I get android.os.NetworkOnMainThreadException when call sentData()
Now I use
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
to resolve the problem, but I want to know how to use asynctask in true way.
This code is for online game that receive message from server in realtime and it can also sent message to server from some activity by calling sentData().
Resolve
Example: Android bi-directional network socket using AsyncTask
You should set the class to be public and no outer class needed.
public class SocketAsync extends AsyncTask<String, String, String> {
private PrintWriter printWriter;
#Override
protected String doInBackground(String... params) {
//You do things here.
}
}
This is how you set up the class and in the Activity class, you simply call this by code like :
new SocketAsync(this).execute(/*Put parameters here.*/);
Hope this will help.
The method SocketAsync.sentData2() calls socket.isConnected(). This is being called on the main thread because something calls SocketThread.sendData2() on the main thread.
Code executes on the thread it is called from. Just because you put that code in your AsyncTask class does not mean it will be on a background thread. Only the code in doInBackground() is guaranteed to be executed in the background, because the Android framework takes care to call that method from a background thread. If you called it yourself, it would execute on whatever thread called it. sentData2() is no different.
You are using Asynctask wrong. When you call the execute method the doInBackground is called which runs on a different thread. You use the onPostExecute method from AsyncTask to get your data back to the main thread (a kind of callback method). This method runs on the main ui thread.
The sendData method would require a new AsyncTask, with a new doInbackground and a new call to execute.
Related
I'm trying to implement a basic login screen for an android app. The flow is as follows:
1) User enters login information and hits submit
2) A LoginRequest which extends AsyncTask is created and executed.
3) The doInBackground will fire some http calls to validate the user credentials
4) The onPostExecute should be getting called to set the loginResults
5) Ui thread sees the login results and continues accordingly.
I'm been simplifying the code to get to the root issue but haven't had any luck so far. Here is the simplified code that still repros the issue.
Inside my activity:
private void tryLogin(String email, String password)
{
this.showProgress(true);
LoginHelper loginHelper = new LoginHelper();
LoginResult result = loginHelper.tryLogin(email, password);
this.showProgress(false);
}
This gets called from my submit buttons on click listener.
Inside LoginHelper:
TestClass test = new TestClass();
public LoginResult tryLogin(String mobileNumber, String password, int deviceId)
{
String loginUrl = "...";
new LoginRequest(test).execute(loginUrl);
while (test.result == null)
{
try {
Thread.sleep(1000);
}
catch (Exception e)
{
//...
}
}
return test.result;
}
This will execute the AsyncTask and wait for the result being continuing.
LoginRequest:
public class LoginRequest extends AsyncTask<String, Void, LoginResult>
TestClass test;
public LoginRequest(TestClass test)
{
this.test = test;
}
#Override
protected LoginResult doInBackground(String... params) {
LoginResult ret = null;
ret = new LoginResult(1,"test");
return ret;
}
#Override
protected void onPostExecute(LoginResult result) {
this.test.result = result;
}
}
I run this through the debugger with breakpoints inside the doInBackground and onPostExecute. The doInBackground executes correctly and returns the LoginResult value, but the onPostExecute breakpoint never gets hit, and my code will wait in the while loop in LoginHelper.
You are basically checking the whole time the variable 'result' of your LoginRequest. But that's not, how AsyncTask works.
From Docs:
AsyncTask allows you to perform asynchronous work on your user
interface. It performs the blocking operations in a worker thread and
then publishes the results on the UI thread, without requiring you to
handle threads and/or handlers yourself.
You can do your work in doInBackground() method and the publish you results in onPostExecute().
onPostExecute runs on UI Thread, to allow you change elements, show the result or whatever you want to do. Your problem is, that you are the whole time blocking the UI Thread with your checking method in tryLogin()
So how to solve it?
Remove the checking method:
public void tryLogin(String mobileNumber, String password, int deviceId)
{
// Starts AsynTasks, handle results there
String loginUrl = "...";
new LoginRequest().execute(loginUrl);
}
in AsyncTask:
public class LoginRequest extends AsyncTask<String, Void, LoginResult>
// Removed Constructor, if you need to pass some other variables, add it again
#Override
protected LoginResult doInBackground(String... params) {
// TODO: Change this to actual Http Request
LoginResult ret = null;
ret = new LoginResult(1, "test");
return ret;
}
#Override
protected void onPostExecute(LoginResult result) {
// Now the result arrived!
// TODO: Use the result
}
}
More Thoughts:
You probably want to store user credentials. If so, make sure the are safe. Link
You might want, depending on results, change some UI. Here's an example:
AsyncTask:
public class LoginRequest extends AsyncTask
private Activity activity;
// Constructor
public LoginRequest(Activity activity) {
this.activity = activity;
}
#Override
protected LoginResult doInBackground(String... params) {
// TODO: Change this to actual Http Request
LoginResult ret = null;
ret = new LoginResult(1, "test");
return ret;
}
#Override
protected void onPostExecute(LoginResult result) {
ActivityLogin acLogin = (ActivityLogin) activity;
if(result.equals("ok")) {
Button loginButton = (Button) acLogin.findViewById(R.id.login-button);
loginButton.setBackgroundColor(Color.GREEN);
//Finish LoginActivity
acLogin.finish();
}
else {
//TODO: Fail Handling
}
}
}
And the start it like this:
new LoginRequest(loginActivity).execute(loginUrl);
I didnt tested the code.
It's AsyncTask so it's calling the LoginRequest and while(test.result) at the same time. You got stuck in the while loop because test.result is not done returning yet. test.result is done in onPostExecute(), so if you move that while loop in that function it will work and onPostExecute() will get called. One way to solve this problem is to implement a callback interface. Put the while loop in the overrided callback method.
refer to my answer here: how to send ArrayList(Bitmap) from asyncTask to Fragment and use it in Arrayadapter
Try This
public class LoginRequest extends AsyncTask<String, Void, LoginResult>
{
TestClass test;
LoginResult ret = null;
public LoginRequest(TestClass test)
{
this.test = test;
}
#Override
protected Boolean doInBackground(String... params) {
ret = new LoginResult(1,"test");
return true;
}
#Override
protected void onPostExecute(Boolean success) {
if(success)
this.test.result = result;
}
}
Temporary solution : You can add this.test.result = result; in the doInbackground() method.
#Override
protected LoginResult doInBackground(String... params) {
LoginResult ret = null;
ret = new LoginResult(1, "test");
this.test.result = result;
return ret;
}
Please post full code to get proper solution.
I have an AsyncTask as public class like below,
public class FetchFromTCWebTask extends AsyncTask<String, Void, String> {
MainActivity mainActivity;
int requestId;
private final static String DEBUG_TAG = "FetchFromTCWebTask";
FetchFromTCWebTask(MainActivity activity, int id) {
mainActivity = activity;
requestId = id;
}
#Override
protected String doInBackground(String[] urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
#Override
protected void onPostExecute(String result) {
mainActivity.loadFinished(result);
}
// Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
public interface UIListner {
public void loadFinished(String result);
public void loading(int progress);
}
}
Now i want to call it as
(new FetchFromTCWebTask(actviity,1)).execute(url);
(new FetchFromTCWebTask(actviity,2)).execute(url);
(new FetchFromTCWebTask(actviity,3)).execute(url);
Now My Question is , there any way to do like it in parallel and get back the results separately for 1,2 and 3 ? if yes then how ?
Use a thread pool to achieve this.
(new FetchFromTCWebTask(actviity, 1)).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url)
(new FetchFromTCWebTask(actviity, 2)).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url)
(new FetchFromTCWebTask(actviity, 3)).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url)
ExecuteOnExecutor
Since I figure you're about to ask me how to write this (for the twentieth time), here's a small example. I omitted some parts since they're not relevant. This is how you would send the id back to the activity to distinguish between them. If you needed to do different tasks inside of onPostExecute then just use a switch/case statement.
public class FetchFromTCWebTask extends AsyncTask<String, Void, String>
{
MainActivity mainActivity;
int requestId;
FetchFromTCWebTask(MainActivity activity, int id)
{
mainActivity = activity;
requestId = id;
}
...
#Override
protected void onPostExecute(String result)
{
mainActivity.loadFinished(result, requestId);
}
public interface UIListner
{
public void loadFinished(String result, int id);
public void loading(int progress);
}
}
Use custom thread pool.
Why not use build in thread pool(AsyncTask.THREAD_POOL_EXECUTOR)? Its got quite small limit of tasks it can handle. How can you tell that build in pool aint working? Its simple, AsyncTask wont start :)
I have an app that makes numerous RESTful service calls. I execute the calls in a class extending Asynctask. If I have to cancel the asynctask, I also want to cancel the service call. Unfortunately, cancelling the async operation still allows doInBackground to complete and I can't call isCancelled() once the request is waiting for a response (which can take a little bit). Right now, from within my doInBackground method I'm registering to be notified from the UI thread if a cancel request is made, so I can abort the HttpResponse object. Here is a piece of sample code.
It has worked so far, but can I really count on it, or am I just getting lucky? Can you count on one thread to call a method in another thread?
public class AsyncTestActivity extends Activity {
private ArrayList<IStopRequestMonitor> monitors;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
public void stopActivity() {
if (monitors == null || monitors.size() < 1) return;
for (int i = 0; i < monitors.size(); i++) {
monitors.get(i).stopRequest();
}
}
public void addListener(IStopRequestMonitor listener) {
if (monitors == null) monitors = new ArrayList<IStopRequestMonitor>();
monitors.add(listener);
}
public void readWebpage(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.mywebsite.com/feeds/rsstest.xml" });
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
DefaultHttpClient client = new DefaultHttpClient();
final HttpGet httpGet = new HttpGet(urls[0]);
addListener(new IStopRequestMonitor() {
public void stopRequest() {
if (httpGet == null) return;
httpGet.abort();
cancel(true);
}
});
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
// handle inputstream
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
#Override
protected void onPostExecute(String result) {
Log.d("Result:", result);
}
}
interface IStopRequestMonitor {
public void stopRequest();
}
}
There is still a race here. If stopActivity() runs before the background thread has called addListener(), the listener will get added later and will never be called to abort the HttpGet.
If you are going to call cancel() from the UI thread (or whatever thread you create the AsyncTask on), you can:
Create a 'private HttpGet httpGet' field in your AsyncTask.
Override onPreExecute() and initialize httpGet there.
Override onCancel() and say 'if (httpGet != null) { httpGet.abort() }'
In doInBackground(), return immediately if isCancelled(), otherwise run.
Because this initializes httpGet on the UI thread, a cancel() call will either run before execute() (and therefore doInBackground will see isCancelled() return true), or it will run after httpGet exists and therefore the HttpGet will be aborted.
You don't need the listeners unless you are using that for something else.
you can define global object of asynctask class and using obj.cancle() method call on button click or whenever you need.
I have an Activity, which starts an AsyncTask with an Implemented process dialog. That works fine! But i want to get a String return when the asyncTask has finished. So i have to return something in the onPostExecute - Method.
That result(string) i want to grab in the Activity, which started the AsyncTask.
I do not want to use .get() because it blocks the UI thread.
What do i have to write into onPostExecute and the Activity grab the string from doInBackground?
Thank you for any kind of help to solve this problem ;)
Now with Code:
class BgDL extends AsyncTask<String, Integer, String> {
String finishString="";
private Context context;
ProgressDialog pdialog;
public BgDL(Context cxt) { //get the context (usually "this" from Activity / otherwise progressdialog wont show up!
context = cxt;
pdialog = new ProgressDialog(context);
}
#Override
protected String doInBackground(String... strings) {
OutputStream output;
ByteArrayOutputStream baos = null;
try {
URL url = new URL(strings[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
if (strings[1]=="toString") { // write byte to string if a file is not given
baos= new ByteArrayOutputStream();
output = new DataOutputStream(baos);
} else { //otherwise to string
output = new FileOutputStream(strings[1]);
}
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
if (strings[1]=="toString") {
finishString = baos.toString(); //
} // only write byte to string if a file is not given
} catch (Exception e) {log.d("ex",e.toString());
}
return finishString;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pdialog.setTitle("Please wait");
pdialog.setIndeterminate(false);
pdialog.setMax(100);
pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pdialog.show();
}
#Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
pdialog.setProgress(progress[0]);
}
protected void onPostExecute(String...finishString) {
pdialog.dismiss();//!!!!!!!!!finishString i want to pass over to my Activity, which started this asynctask with .execute();
}
Create a class in your project which extends activity as shown below:
public class SomeClass extends Activity
{
public void dataFromPostExecute(String data)
{
System.out.println("in mainactivity");
}
}
If you want a single thread for every activity, just create a class which extends
Application
public class Async extends Application
{
private Socket globalSocket;
#Override
public void onCreate()
{
//socket = null;
}
public Socket getglobalSocket()
{
return globalSocket;
}
public void setGlobalSocket(Socket globalSocket)
{
this.globalSocket = globalSocket;
}
}
In your socket class which extends Asynctask do the following:
public SocketClass extends AsyncTask<String,String,String>
{
Async app;
private SomeClass t_act;
public SocketClass(SomeClass sct)
{
t_act = sct;
this.con = tst;
app= ((Async)sct.getApplicationContext());
}
protected void onPostExecute(String data)
{
t_act.dataFromPostExecute(data);
}
}
Then, in your activity extend SomeClass and do as shown below:
public class Activity1 extends SomeClass
{
public void dataFromPostExecute(String data)
{
//do whatever you want. "data" of this method contains the values from
postexecute()
}
}
Your return value from doInBackground() is you formal in onPostExecute().
So you should just be able to pass it in.
What do i have to write into onPostExecute and the Activity grab the
string from doInBackground?
When you are using AsyncTask then you can update your UI only on onProgressUpdate and onPostExecute method.
Your doInBackground() method returns some data and these data is going to onPostExecute method(it depends also how your generic are declared).
Generally, there is no another approaches how to do it.
You meant this:
AsyncTask a = new AsyncTask(Context);
a.execute(Input);
First means that your constructor looks like
public MyAsync(Context c) {
this.c = c;
}
Second means that you declared your first generic type(assumption Input param is String) as
private class MyAsync extends AsyncTask<String, Void, String> {
//...
}
And you want to update UI with String that returns doInBackground() method and merely place is onPostExecute method with IN parameter String that returns doInBackground().
protected void onPostExecute(String stringReturnedFromDoInBackground)
{
// some work
}
So if you want to do it in different way, change your application logic and use for example ResultReceiver with IntentService.
I am new to Android development and Java and was wondering if somebody could help me with the following:
I have created an application that runs a server thread listening on a specified port. I would like to print messages received from a connected client into a TextView in the activity.
The server thread is in a separate class. The run method in this class listens for a client connection and reads any data received into a String.
What would be the best way for me to transfer the contents of this String back to the activity so that it can update the TextView?
From my (limited) understanding, only the ui thread should update a TextView and I can't find a way to get runOnUiThread to update the TextView.
Added code as requested.
Activity code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView messages = (TextView) findViewById(R.id.messages);
try {
newThread server = new newThread(this, messages);
} catch(Exception e) {
Toast.makeText(ChatActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
Run method in newThread class:
public void run()
{
serv = new ServerSocket(8000);
while(true)
{
cli = serv.accept();
user = cli.getInetAddress().toString();
BufferedReader cli_in = new BufferedReader(new InputStreamReader(cli.getInputStream()));
OutputStreamWriter cli_out = new OutputStreamWriter(cli.getOutputStream());
while((buf = cli_in.readLine()) != null)
{
// Update the messages TextView with buf
}
}
}
To avoid making things too cluttered I have omitted what irrelevant code I can.
Basically, in the inner while loop in run() I would like to pass the "buf" String to the activity so that the messages textview can be updated with it's content.
Cheers
Maybe a bad idea, but how about using AsyncTask? Didn't try if this would work, but it just might, since onProgressUpdate has access to UI thread.
private TextView messages;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
messages = (TextView) findViewById(R.id.messages);
ReceiveTask receive = new ReceiveTask();
receive.execute(100)
}
private void updateTextView(String text)
{
messages.setText(text);
}
private class ReceiveTask extends AsyncTask<Integer, String, Long> {
#Override
protected void onPreExecute() {
}
protected Long doInBackground(Integer... urls) {
newThread nt = new newThread();
while(true)
{
publishProgress(run());
}
return (long)0;
}
protected void onProgressUpdate(String... value) {
updateTextView(value[0]); //method in Activity class, to update TextView
}
protected void onPostExecute(Long result) {
}
}
Basically publishProgress will send data to onProgressUpdate, which will then send data to method (updateTextView) in main class and update TextView.
Usually it helps if you tell people you're working on a chat. Also, run() will need to be modified, to return string back, and remove while(true) loop from it. This is NOT the best idea, I suggest you go through a few tutorials on how to make an android chat first.