android asynctask NetworkOnMainThreadException - android

I'm trying to write a server/client app in android using sockets and i handle the client socket in AsyncTask (server is not android, just ordinary java).I get the exception when I'm trying to read from the server.I found out that when I delete android:targetSdkVersion="16" from android manifest the exception goes away and I can read from the server.
I don't understand why is that? could anyone help me clarify this? I also have problems understanding how the asynctask method doInBackground and my own methods relate. Does conhandler.execute() run doInBackground() and then just waits until I call the other methods? thanks for help.
public class ConnectionHandler extends AsyncTask<Void, Void, Void>{
public static String serverip = "10.0.2.2";
public static int serverport = 5000;
Socket s;
PrintWriter out;
BufferedReader in;
protected Void doInBackground(Void... params) {
try {
s = new Socket(serverip, serverport);
Log.i("AsyncTank", "doInBackgoung: Created Socket");
}...
if (s.isConnected()) {
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream(), true);
Log.i("AsyncTank", "doInBackgoung: Socket created, Streams assigned");
} ....
}
public void writeToStream(String message) {
try {
if (s.isConnected()){
out.println(message);
} else {
Log.i("AsynkTask", "writeToStream : Cannot write to stream, Socket is closed");
}
} catch (Exception e) {
Log.i("AsynkTask", "writeToStream : Writing failed");
}
}
public String readFromStream() {
try {
if (s.isConnected()) {
Log.i("AsynkTask", "readFromStream : Reading message");
String ret=in.readLine();
Log.i("AsynkTask", "readFromStream : read "+ret);
return ret;
} else {
Log.i("AsynkTask", "readFromStream : Cannot Read, Socket is closed");
}
} catch (Exception e) {
Log.i("AsynkTask", "readFromStream : Reading failed"+e.getClass());
}
return null;
}
}
this is my main activity
public class MainActivity extends Activity {
private EditText view_email;
private EditText view_password;
TextView result;
ConnectionHandler conhandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conhandler = new ConnectionHandler();
conhandler.execute();
}
public void register(View view) {
view_email= (EditText) findViewById(R.id.email);
view_password = (EditText) findViewById(R.id.password);
String email=view_email.getText().toString();
String password=view_password.getText().toString();
conhandler.writeToStream("register");
conhandler.writeToStream(email);
conhandler.writeToStream(password);
String res=conhandler.readFromStream(); //here's the exception
result=(TextView) findViewById(R.id.result);
result.setText(res);
}
}

There are two Solution of this Problem.
1) Don't write network call in Main UIThread, Use Async Task for that.
2) Write below code into your MainActivity file after setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
And below import statement into your java file.
import android.os.StrictMode;

android.os.NetworkOnMainThreadException
this eror comes With HoneyComb(3.0 or Later).
you can not perform a networking operation on its main thread as documentation says. to getting ride of this you must use handler or asynctask. AFAIK There is no another way to do it.
you can See this for More Details WHY ICS Crashes your App
Try Using Below Code Snippet
new Thread(){
public void run(){
//do your Code Here
}
}.start();

Related

listen to an io socket event with an Android client

first of all excuse my english, because i am not good in english. I'm looking for a way to retrieve information in android send from a nodeJS server with socket io. I have the impression that it is a problem of version. my code compiles very well, I do not manage to retrieve the information contained in a JS object.
here is the server code
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var data ={"msg": "hello i am the server"};
io.on('connection',function(socket){
console.log('one user connected '+socket.id);
socket.on('message',function(obj){
console.log(obj.text);
socket.emit('take', data);
})
socket.on('disconnect',function(){
console.log('one user disconnected '+socket.id);
})
})
http.listen(3000,function(){
console.log('server listening on port 3000');
})
the information I'm looking for is 'data'
here is the java code I use, I prefer to put the whole code in case the problem is elsewhere
public class MainActivity extends AppCompatActivity {
private Socket socket;
public String ReceiveMsg ="message par defaut ";
{
try {
socket = IO.socket("http://192.168.43.168:3000");
socket.connect();
} catch (URISyntaxException e) {
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = (EditText) findViewById(R.id.editText);
TextView textView = (TextView) findViewById(R.id.textView);
Button but = (Button) findViewById(R.id.button);
socket.connect();
String msg = takemsg(editText);
sendMsg(msg);
socket.on("take", handleIncomingMessages);
textView.setText(getAz(ReceiveMsg));
}
public String takemsg(EditText editText){
String msg = "message par defaut ";
msg = editText.getText().toString();
return msg;
}
public void sendMsg(String msg ){
JSONObject obj = new JSONObject();
try{
obj.put("text", msg);
socket.emit("message", obj);
}catch (JSONException e){
}
}
private Emitter.Listener handleIncomingMessages = new Emitter.Listener(){
#Override
public void call(final Object... args){
runOnUiThread(new Runnable() {
#Override
public void run() {
JSONObject data = (JSONObject) args[0];
try {
ReceiveMsg = data.getString("msg");
} catch (JSONException e) {
}
}
});
}
};
}
First I think we need more data.
What are the outputs of the server? Is connecting well?, check if your code in android is running.
If it's not connecting try setting the port with opts
...
try {
IO.Options opts = IO.Options();
opts.port = 3000;
socket = IO.socket("http://192.168.43.168", opts);
socket.connect();
} catch (URISyntaxException e) {
...
check if your service is alive. for services exists some returns that allows to maintain your service working.
https://developer.android.com/reference/android/app/Service.html#START_STICKY
we must put in the manifest
<uses-permission android:name="android.permission.INTERNET" />
to allow the application to use the network

Set text in thread class from activity

I have two classes, one is Activity, and one is Thread class:
Activity:
public class MainActivity extends AppCompatActivity {
ObjectInputStream inFromServer = null;
ObjectOutputStream outToServer=null;
Socket clientSocket = null;
String userName;
EditText message;
static TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
message = (EditText)findViewById(R.id.txtMessage);
textView = (TextView)findViewById(R.id.textView);
try {
clientSocket = new Socket("10.0.0.41", 10002);
outToServer =
new ObjectOutputStream(clientSocket.getOutputStream());
inFromServer =
new ObjectInputStream((clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
ReceiveMessages receiveMessages = new ReceiveMessages(inFromServer);
receiveMessages.start();
}
public void onClickSend(View view) throws IOException {
outToServer.writeObject("Eliran!" + message.getText().toString());
}
public void btnUserName(View view) throws IOException {
EditText editText = (EditText)findViewById(R.id.userName);
userName = editText.getText().toString();
outToServer.writeObject(userName + "!");
}
}
Thread class:
public class ReceiveMessages extends Thread {
ObjectInputStream inFromServer = null;
public ReceiveMessages(ObjectInputStream inFromServer)
{
this.inFromServer = inFromServer;
}
public void run() {
try {
while (true) {
String message = (String) inFromServer.readObject();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
I want to set text in the text view of the activity in the Thread class.
How can I do it? InflateLayout isn't helping I think.
I would go about it by creating a Callback that gets called when the String is received from the aerver.
interface ReceiveMessageListener {
void onMessageReceived (String message);
}
Then have my Activity implement the interface and set the text in the onMessageReceived call back.
#Override
public void onMessageReceived (String message) {
runOnUiThread(new Runnable() {
#Override
public void run () {
textView.setText(message);
}
});
}
You'll also have to change the constructor for your MessageReceive class to take a MessageReceiveListener.
From your Activity:
ReceiveMessages receive = new ReceiveMessages(input, this);
Only the UI thread can interact with ui elements (such textview). Try using runOnOiThread or mainHandler.post ()

Socket.isConnected() make my android app force close

I don't know what happen with my source code about Socket in Android, when I use method
.isConnected()
My app always force close. And here my source code
public class MyActivity extends Activity {
private String IP;
private int PORT;
private Socket socket;
private PrintWriter printWriter;
private TextView text;
private EditText fieldIp;
private EditText fieldPort;
private Button connect;
private FrameLayout frameIP;
private String message;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
frameIP = (FrameLayout)findViewById(R.id.frameIP);
connect = (Button)findViewById(R.id.connect);
fieldIp = (EditText)findViewById(R.id.ip);
fieldPort = (EditText)findViewById(R.id.port);
text = (TextView)findViewById(R.id.keterangan);
connect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IP = fieldIp.getText().toString();
PORT = Integer.parseInt(fieldPort.getText().toString());
SocketConnect socketConnect = new SocketConnect(IP,PORT);
socketConnect.execute();
}
});
}
private class SocketConnect extends AsyncTask<Void, Void, Boolean> {
String ip;
int port;
public SocketConnect(String a, int b){
this.ip = a;
this.port = b;
}
#Override
protected Boolean doInBackground(Void... params) {
try {
socket = new Socket();
socket.connect(new InetSocketAddress(ip,port));
if(socket.isConnected())
{
text.setText("Connected!");
}
else
{
text.setText("Failed to connect!");
}
} catch (IOException e) {
Log.e("MyActivity",e.getMessage());
}
finally {
startActivity(new Intent(getApplicationContext(),ListViewText.class));
}
return null;
}
}
}
And I use this in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
I hope you can help me guys :(
Change the doInBackground method as follows...
#Override
protected Boolean doInBackground(Void... params) {
boolean success = true;
try {
socket = new Socket();
socket.connect(new InetSocketAddress(ip, port));
} catch (Exception e) {
success = false;
Log.e("MyActivity", e.getMessage());
}
return success;
}
Then add an onPostExecute method...
#Override
protected void onPostExecute(boolean result) {
if(result) {
text.setText("Connected!");
startActivity(new Intent(MyActivity.this, ListViewText.class));
}
else {
text.setText("Failed to connect!");
}
}
First thing you are calling UI operation outside of UI thread (that is why AsyncTask was created, to handle background job only in doInBackground) So problem about displaying text un TextView is solved...
But more important thing:
Never open Socket in AsyncTask. On Android developer site you can find following:
If you need to keep threads running for long periods of time, it is
highly recommended you use the various APIs provided by the
java.util.concurrent package such as Executor, ThreadPoolExecutor and
FutureTask.)
And that is exactly what you want to do. So use Service, Thread or those mentioned above instead.

AsyncTask in an android socket application

i am making an android socket app to communicate with the server for creating accounts, and i noticed i have to do this in AsyncTask sub class, even when i seperate it to another class without UI,but i am terribly confused how can i use AsyncTask on this, is there any one expert here who can help me please?
this is the code:
public class AccountCreator extends Activity {
public AccountCreator(){
super();
}
// for I/O
ObjectInputStream sInput; // to read from the socket
ObjectOutputStream sOutput; // to write on the socket
Socket socket;
public static String LOGTAG="Lifemate";
public String server = "localhost";
public String username = "user";
public String password = "rezapassword" ;
public int port = 1400;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(LOGTAG,"oncreate called");
this.start();
}
AccountCreator(String server, int port, String username,String password) {
this.server = "localhost";
this.port = 1400;
this.username = username;
Log.i(LOGTAG,"first accountcreator called");
}
public boolean start() {
// try to connect to the server
//this method returns a value of true or false when called
try {
socket = new Socket(server, port);
}
// if it failed not much I can so
catch(Exception ec) {
// display("Error connectiong to server:" + ec);
Log.i(LOGTAG,"Error connectiong to server:" + ec);
return false;
}
String msg = "Connection accepted " + socket.getInetAddress() + ":" +
socket.getPort();
// display(msg);
Log.i(LOGTAG, msg);
/* Creating both Data Stream */
try
{
sInput = new ObjectInputStream(socket.getInputStream());
sOutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException eIO) {
// display("Exception creating new Input/output Streams: " + eIO);
Log.i(LOGTAG,"Exception creating new Input/output Streams: " +
eIO);
return false;
}
// creates the Thread to listen from the server
// Send our username to the server this is the only message that we
// will send as a String. All other messages will be ChatMessage objects
try
{
sOutput.writeObject(username);
sOutput.writeObject(password);
}
catch (IOException eIO) {
// display("Exception doing login : " + eIO);
Log.i(LOGTAG,"Exception doing login : " + eIO);
disconnect();
return false;
}
// success we inform the caller that it worked
return true;
}
// private void display(String msg) {
// TextView screenprint = (TextView)findViewById(R.id.systemmessages);
// screenprint.setText(msg);
// }
private void disconnect() {
Log.i(LOGTAG,"reached disconnect");
try {
if(sInput != null) sInput.close();
}
catch(Exception e) {} // not much else I can do
try {
if(sOutput != null) sOutput.close();
}
catch(Exception e) {} // not much else I can do
try{
if(socket != null) socket.close();
}
catch(Exception e) {} // not much else I can do
}
public void Begin() {
Log.i(LOGTAG,"it begun");
int portNumber = 1400;
String serverAddress = server;
String userName = username;
String newpassword = password;
AccountCreator accountcreator = new AccountCreator(serverAddress, portNumber,
userName,password);
if(!accountcreator.start())
return;
}
}
i was trying to put whole code in Async, i dont know if was i right, do i need to do that also or just some parts of it?
In brief, AsyncTask contains a few methods which may be helpful:
onPreExecute:
This method is the first block of code executed when calling asyncTask.execute(); (runs on mainUIThread).
doInBackground:
Here you put all the code which may suspend you main UI (causes hang for your application) like internet requests, or any processing which may take a lot of memory and processing. (runs on background thread), contains one parameter taken from the asyncTask.execute(ParameterType parameter);
onPostExecute
Runs after doInBackground(). Its parameter is the return value of the doInBackground function, and mainly you put the changes in UI need to be done after the connection is finished (runs on mainUIThread)
You have to declare another class within the class you have already created.
class SomeName extends Async<Void, String, Void>{
protected void OnPreExecute(){
// starts the task runs on main UI thread
// Can be used to start a progress dialog to show the user progress
}
#Override
protected Void doInBackground(Void... params){
// does what you want it to do in the background
// Connected to the server to check a log-in
return result;
}
protected void OnPostExecute(Void result){
// finishes the task and can provide information back on the main UI thread
// this would be were you would dismiss the progress dialog
}
}

NetworkOnMainThreadException in asynctask [Resolve]

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.

Categories

Resources