当我尝试向服务器发送请求时,我的应用程序崩溃了
My app crashes when I try to send a request to the server
好吧,它实际上在我的 android 工作室模拟器上工作得很好,但是当我尝试 运行 它在我的 phone 上时它就崩溃了。
我只想向服务器发送一个号码,并获得包含我对该号码所需的数据的响应。所以这是我的代码:
thread = new Thread() {
@Override
public void run() {
//server stuff
try {
//Connecting
if(!userClass.equals("")) {
Log.i(debugString, "Attempting to connect to server");
socket = new Socket(hostname, portnumber);
Log.i(debugString, "Connection established!");
BufferedWriter bw = new BufferedWriter((new OutputStreamWriter(socket.getOutputStream())));
bw.write("" + userClass);
bw.newLine();
bw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
input = br.readLine();
}
} catch (IOException e) {
Log.e(debugString, e.getMessage());
} finally {
threadComplete = true;
}
}
};
thread.start();
while(!threadComplete)
continue;
然后每当我想为我的请求获取更新信息时,我就使用这个线程:
String getUserClass = userClass;
if(!getUserClass.equals(""))
{
threadComplete = false;
userClass = getUserClass;
thread.start();
while (!threadComplete)
continue;
changes.setText(input);
}
else Toast.makeText(this, "Error, choose your class", Toast.LENGTH_SHORT).show();
顺便说一句,在每个线程的末尾(在模拟器上,因为在我的 phone 上它崩溃了)我收到一条消息:
Skipped 91 frames! The application may be doing too much work on its main thread.
我还有另一个问题,我也在后台使用 IntentService
到 运行 我的应用程序服务,显然我不希望它永远 运行,所以我做了一个循环,在每个循环的末尾包含一个 wait()
命令,但问题是当我设置等待时间超过 3000 毫秒左右时,服务崩溃了。
我的后台服务代码:
synchronized (this) {
int count = 0;
while (count<4) {
try {
wait(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (notifications && !userClass.equals("")) {
new Thread() {
@Override
public void run() {
//server stuff
try {
//Connecting
if (!userClass.equals("")) {
Log.i("debug", "Attempting to connect to server");
socket = new Socket(hostname, portnumber);
Log.i("debug", "Connection established!");
BufferedWriter bw = new BufferedWriter((new OutputStreamWriter(socket.getOutputStream())));
bw.write("" + userClass);
bw.newLine();
bw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
input = br.readLine();
}
} catch (IOException e) {
Log.e("debug", e.getMessage());
} finally {
complete = true;
}
}
}.start();
while (!complete)
continue;
Toast.makeText(this, "" + input, Toast.LENGTH_SHORT).show();
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.chanka)
.setContentTitle("ביטול שיעורים: ")
.setContentText(input);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
mNotificationId++;
Toast.makeText(this, "" + input, Toast.LENGTH_SHORT).show();
count++;
}
}
}
下面这段代码是罪魁祸首 -
while (!threadComplete)
continue;
你有点把主线程放在一个长循环上。 Android 不允许这样做。这些用例中的一般构造是这样的 -
Step 1 - Show a progress dialog to the user indicating that you are
doing something important and user needs to wait till that is
complete. Show some meaningful text in the progress dialog which makes
sense to the user.
Step 2 - Start a async connection to the server. There are lot of
options in Android to do this. But for your purpose AsyncTask
might
be useful. Connect to your server, fetch and parse data in the
doInBackground
method of AsyncTask
and once the task is complete,
let onPostExecute
publish the same to the Main thread.
Step 3 - Once you get back the result from the Async task, you may
dismiss the progress dialog and continue with whatever you were doing.
请注意,任何时候都不应该阻塞主线程。这是应用程序的事件处理线程,处理所有事件(用户发起或系统发起)。如果线程被阻塞,你会得到你现在看到的那种错误。特别是在您的情况下,Android 系统由于 while 循环而无法执行某些绘制操作。
创建一个新的 Asynctask 和运行其中的套接字建立代码:)
socket = new Socket(hostname, portnumber);
好吧,它实际上在我的 android 工作室模拟器上工作得很好,但是当我尝试 运行 它在我的 phone 上时它就崩溃了。 我只想向服务器发送一个号码,并获得包含我对该号码所需的数据的响应。所以这是我的代码:
thread = new Thread() {
@Override
public void run() {
//server stuff
try {
//Connecting
if(!userClass.equals("")) {
Log.i(debugString, "Attempting to connect to server");
socket = new Socket(hostname, portnumber);
Log.i(debugString, "Connection established!");
BufferedWriter bw = new BufferedWriter((new OutputStreamWriter(socket.getOutputStream())));
bw.write("" + userClass);
bw.newLine();
bw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
input = br.readLine();
}
} catch (IOException e) {
Log.e(debugString, e.getMessage());
} finally {
threadComplete = true;
}
}
};
thread.start();
while(!threadComplete)
continue;
然后每当我想为我的请求获取更新信息时,我就使用这个线程:
String getUserClass = userClass;
if(!getUserClass.equals(""))
{
threadComplete = false;
userClass = getUserClass;
thread.start();
while (!threadComplete)
continue;
changes.setText(input);
}
else Toast.makeText(this, "Error, choose your class", Toast.LENGTH_SHORT).show();
顺便说一句,在每个线程的末尾(在模拟器上,因为在我的 phone 上它崩溃了)我收到一条消息:
Skipped 91 frames! The application may be doing too much work on its main thread.
我还有另一个问题,我也在后台使用 IntentService
到 运行 我的应用程序服务,显然我不希望它永远 运行,所以我做了一个循环,在每个循环的末尾包含一个 wait()
命令,但问题是当我设置等待时间超过 3000 毫秒左右时,服务崩溃了。
我的后台服务代码:
synchronized (this) {
int count = 0;
while (count<4) {
try {
wait(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (notifications && !userClass.equals("")) {
new Thread() {
@Override
public void run() {
//server stuff
try {
//Connecting
if (!userClass.equals("")) {
Log.i("debug", "Attempting to connect to server");
socket = new Socket(hostname, portnumber);
Log.i("debug", "Connection established!");
BufferedWriter bw = new BufferedWriter((new OutputStreamWriter(socket.getOutputStream())));
bw.write("" + userClass);
bw.newLine();
bw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
input = br.readLine();
}
} catch (IOException e) {
Log.e("debug", e.getMessage());
} finally {
complete = true;
}
}
}.start();
while (!complete)
continue;
Toast.makeText(this, "" + input, Toast.LENGTH_SHORT).show();
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.chanka)
.setContentTitle("ביטול שיעורים: ")
.setContentText(input);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
mNotificationId++;
Toast.makeText(this, "" + input, Toast.LENGTH_SHORT).show();
count++;
}
}
}
下面这段代码是罪魁祸首 -
while (!threadComplete)
continue;
你有点把主线程放在一个长循环上。 Android 不允许这样做。这些用例中的一般构造是这样的 -
Step 1 - Show a progress dialog to the user indicating that you are doing something important and user needs to wait till that is complete. Show some meaningful text in the progress dialog which makes sense to the user.
Step 2 - Start a async connection to the server. There are lot of options in Android to do this. But for your purpose
AsyncTask
might be useful. Connect to your server, fetch and parse data in thedoInBackground
method ofAsyncTask
and once the task is complete, letonPostExecute
publish the same to the Main thread.Step 3 - Once you get back the result from the Async task, you may dismiss the progress dialog and continue with whatever you were doing.
请注意,任何时候都不应该阻塞主线程。这是应用程序的事件处理线程,处理所有事件(用户发起或系统发起)。如果线程被阻塞,你会得到你现在看到的那种错误。特别是在您的情况下,Android 系统由于 while 循环而无法执行某些绘制操作。
创建一个新的 Asynctask 和运行其中的套接字建立代码:)
socket = new Socket(hostname, portnumber);