App not responding while transfer data - android

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.

Related

Connect/disconnect operations inside another thread

I defined connection variable in top of the main class:
private XMPPConnection connection;
I am connecting to server with following code:
public void connect(final String username,final String password) {
Thread t=new Thread(new Runnable() {
#Override
public void run() {
ConnectionConfiguration connConfig=new ConnectionConfiguration("server ip", 5222,"localhost");
XMPPConnection connection=new XMPPTCPConnection(connConfig);
try{
connection.connect();
}catch(XMPPException ex) {
setConnection(null);
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.login(username,password);
} catch (SaslException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setConnection(connection);
}
});
t.start();
}
You see I am using thread for this operation.And if connection was successful setConnection method is calling.
setConnection method:
public void setConnection(XMPPConnection connection) {
this.connection=connection;
if (connection != null) {
//Other stuff
....
So,I am setting connection variable inside thread.But when I want to disconnect from server i am getting crash report.
Disconnect code:
try{
connection.disconnect();
}catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Report:
01-27 14:14:05.162: E/XMPPConnection(3160): Error in listener while closing connection
01-27 14:14:05.162: E/XMPPConnection(3160): android.os.NetworkOnMainThreadException
01-27 14:14:05.162: E/XMPPConnection(3160): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
01-27 14:14:05.162: E/XMPPConnection(3160): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-27 14:14:05.162: E/XMPPConnection(3160): at java.net.InetAddress.getLocalHost(InetAddress.java:365)
01-27 14:14:05.162: E/XMPPConnection(3160): at org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy.<init>(Socks5Proxy.java:108)
I know this error.Android doesn't allow to use network operations from ui thread.But I already set connection inside another thread.Why do i need one more thread for disconnect operation ?
I followed this tutorial:http://developer.samsung.com/technical-doc/view.do?v=T000000119
And in this tutorial they didn't use another thread for disconnect.Why I am getting this error ?
Using you code which you have posted. define strict mode thread policy permission in onCreate method of Activity or Application
Assuming you have given INTERNET permission in manifest
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
If you dont wants to use the code which you have posted thn alternate solutions for that below i hve mentioned
Use Async task for the Network related operation or long process related
public class MyActivity extends Activity {
XMPPConnection connection;
private String username;
private String password;
public void connect(final String username, final String password) {
ConnectionConfiguration connConfig = new ConnectionConfiguration(
"server ip", 5222, "localhost");
connection = new XMPPTCPConnection(connConfig);
try {
connection.connect();
} catch (XMPPException ex) {
setConnection(null);
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.login(username, password);
} catch (SaslException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class XMPPConnection extends AsyncTask<Void, Void, Void> {
/*
* (non-Javadoc)
*
* #see android.os.AsyncTask#doInBackground(Params[])
*/
#Override
protected Void doInBackground(Void... params) {
connect(username, password);
return null;
}
/*
* (non-Javadoc)
*
* #see android.os.AsyncTask#onPreExecute()
*/
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
/*
* (non-Javadoc)
*
* #see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// from here update the ui and make ui related changes once
// onpostExecute is called
setConnection(connection);
}
}
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.test_layout);
username = "username#chat.abc.com";
password = "1234567";
new XMPPConnection().execute();
}
}
also Please check this
For async and ui changes , network related operation on main thread related please check this
and make sure setConnection(XMPPConnection connection) you are not doing any UI update related things..
OR you can also try
XMPPTCPConnection connection;
#Override
protected void onResume() {
super.onResume();
connect(LOGGED_USERNAME,"pass");
}
#Override
public void onPause() {
super.onPause();
try{
connection.disconnect();
}catch (SmackException e) {
e.printStackTrace();
}
}
private Handler handler = new Handler() {
/* (non-Javadoc)
* #see android.os.Handler#handleMessage(android.os.Message)
*/
#Override
public void handleMessage(Message msg) {
setConnection(connection);
}
};
public void connect(final String username, final String password) {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
ConnectionConfiguration connConfig = new ConnectionConfiguration(
"server ip", 5222, "localhost");
connection = new XMPPTCPConnection(connConfig);
try {
connection.connect();
} catch (XMPPException ex) {
setConnection(null);
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.login(username, password);
} catch (SaslException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.sendEmptyMessageAtTime(0, 1000);
}
});
t.start();
}
public void setConnection(XMPPTCPConnection connection) {
this.connection=connection;
if (connection != null) {
....

Android Software caused connection abort and Async task

I use a bluetooth_access Async task to establish a connection and I need continue to use the input stream and out stream of the bluetooth socket I established.
The issue I having is when I clicked button1 or button2, it sometimes (not all the times) cause Software caused connection abort. It got tripped on out.write(bytes) when clicked.
public class MainActivity extends Activity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)this.findViewById(R.id.button1);
button2 = (Button)this.findViewById(R.id.button2);
button3 = (Button)this.findViewById(R.id.button3);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
try {
for (int i = 0; i < 3; i++) {
test();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean connected = false;
private BluetoothSocket sock;
private InputStream in;
private OutputStream out;
private Button button1;
private Button button2;
private Button button3;
private TextView data_t;
private BufferedReader in_read;
public void test() throws Exception {
if (connected) {
return;
}
new bluetooth_access().execute("");
}
#Override
public void onClick(View v){
// TODO Auto-generated method stub
if(v.equals(button1)){
String s = "turn left";
byte[] bytes=s.getBytes();
try {
out.write(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(v.equals(button2)){
String s = "turn right";
byte[] bytes=s.getBytes();
try {
out.write(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//new post_data().execute("");
}
if(v.equals(button3)){
String s = "read ADC";
byte[] bytes=s.getBytes();
try {
out.write(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class bluetooth_access extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
BluetoothDevice Pi = BluetoothAdapter.getDefaultAdapter().
getRemoteDevice("00:15:83:0C:BF:EB");
Method m=null;
try {
m = Pi.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
sock = (BluetoothSocket)m.invoke(Pi, Integer.valueOf(1));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
sock.connect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("PiTest", "++++ Connected");
return null;
}
#Override
protected void onPostExecute(String result) {
;
TextView text = (TextView) findViewById(R.id.textView5);
text.setText("Connected through Bluetooth");
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
// original
try {
in = sock.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out=sock.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
You are trying to write to a socket stream from your main UI thread (from onClick), i would consider doing all socket communication from asynctask itself, or another thread if needed.
your connected variable is not being set, not sure if that was intentional, it can cause multiple execution of your bluetooth asynctask.

InetAddress.getByName () adds / to the ip

I'm having a weird problem. I'm creating a socket and giving it the IP 192.168.43.255. When I use InetAddress.getByName(IP) it adds / to the ip as shown in the log below. Why this is happening ??
here is my code
public class ServerCom extends AsyncTask<String, Void , String>{
private int port=9999;
private String IP="192.168.43.255";
private BufferedReader input;
private Socket socket;
private DataOutputStream toSer;
private InetAddress serverAddr;
private String LocationID;
File file;FileWriter writer;
#Override
protected String doInBackground(String... RSS) {
// TODO Auto-generated method stub
Log.d("s","async");
SandboxView.Locate=false;
try {
serverAddr = InetAddress.getByName(IP);
socket = new Socket(serverAddr, port);
toSer = new DataOutputStream(socket.getOutputStream());
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// send result to server
try {
toSer.writeBytes(RSS[0]+"\n");
//get the response from server
LocationID=input.readLine();
toSer.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return LocationID;
}
protected void onPostExecute(String result)
{
super.onPostExecute(result);
//dismiss progressdialog.
//update ui
MainActivity.LocationID=LocationID;
SandboxView.Localization=MainActivity.CH2.getLocation(LocationID);
try {
writer = new FileWriter(file, true);
writer.append("room :"+LocationID+"\n");
writer.append(SandboxView.Localization.x+" "+SandboxView.Localization.y+"\n");
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MainActivity.view.invalidate();
SandboxView.Locate=true;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
}
05-16 17:53:16.331: W/System.err(3495): java.net.ConnectException: /192.168.43.255:9999 - Network is unreachable
This isn't really adding a / to the IP address, but the format of the address output by InetAddress.toString() uses a / to separate the hostname from the host address. See here. So, the / isn't really being added to the address, it is just shown in the logging.

Communicating with a server using Asynctask when the timertask finishes

I'm using a TimerTask to get the result of a Wi-Fi scan on a specific interval. I want to send the scanning results to a server.So I used AsyncTask to send the results. When I call the AsyncTask from inside the TimerTask my application crashes. Can anyone tell me why this is happening? Also in my code what is the best way to send the result to a server?
Here is my code:
public class ServerComm extends AsyncTask<String, Void, String>{
int port=9999;
String IP="192.168.2.100";
BufferedReader input;
#Override
protected String doInBackground(String... scanRes) {
// TODO Auto-generated method stub
Socket socket;
String loc="";
FileWriter writer;
File sock=new File(Environment.getExternalStorageDirectory()+"/network.txt");
try {
writer = new FileWriter(sock, true);
writer.append("in async task.\n");
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
InetAddress serverAddr = InetAddress.getByName(IP);
socket = new Socket(serverAddr, port);// socket is created
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.d("ser","socket");
// now send
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println(scanRes);
// get the reply
loc = input.readLine();
//close
socket.close();
return loc;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return loc;
}
#Override
protected void onPostExecute(String result) {
// draw the new location on the map
}
This is the code of the TimerTask:
public void Locate()
{
if(isScanning)// if a previous scan is running cancel it
{
timer.cancel();
}
else
{
timer.schedule(new TimerTask() { // start new scanner
#Override
public void run() {
// TODO Auto-generated method stub
if(counter==numOfScans)
{
try {
writer = new FileWriter(file, true);
writer.append(RSS+" \n");
writer.append("Finished Collecting RSSIs.\n");
writer.close();
new ServerComm().execute(RSS);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
timer.cancel();
}
else // not finished
{
isScanning=true;
if(result!=null)
{
if(result.size()!=0)
{
for(int i=0;i< result.size();i++)
{
RSS=RSS+result.get(i).BSSID+" "+result.get(i).level+" ";
}//end for
counter++;
} // end of if
}//end of else
}
}
}, 0,Interval);
}
//get location
}
I solved the problem, so I'm posting the answer in case someone else faced the same problem.
The problem was the AsyncTask should not be called from TimerTask. A handler should be used to run the AsyncTask Like this:
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
public void run() {
new ServerComm().execute(RSS);
}
});
This is solution is by arthvading on this question.

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

Categories

Resources