当我尝试向服务器发送请求时,我的应用崩溃了
嗯,它在我的android studio模拟器上运行得非常好,但当我尝试在手机上运行它时,它只会崩溃。 我只想发送一个号码到服务器,并获得我需要的数据的响应。所以这是我的代码,这样做:当我尝试向服务器发送请求时,我的应用崩溃了
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();
顺便说一句,在每次结束线程(在模拟器上因为我的手机崩溃),我得到一个消息: 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不允许这样做。在这些类型的用例的一般结构是这样的 -
第1步 - 显示表明您 做一些重要的,用户需要等到即 完成进度对话框给用户。在进度对话框中显示一些有意义的文本,使 对用户有意义。
第2步 - 启动到服务器的异步连接。 Android中有很多 选项可以做到这一点。但为了您的目的
AsyncTask
可能 会有用。连接到您的服务器,获取并解析doInBackground
方法AsyncTask
中的数据,一旦任务完成, 让onPostExecute
发布到主线程。第3步 - 一旦找回异步任务的结果,您可以退出进度对话框并继续进行任何操作。
请注意,主线程不应该在任何时候被阻止。这是应用程序的事件处理线程,并处理所有事件(用户发起或系统启动)。如果线程被阻塞,你会得到你现在看到的那种错误。特别是在你的情况下,由于while循环,Android系统无法执行一些绘制操作。
好吧,它运作良好,但我仍然有问题,我的服务停止,如果我设置每次检查之间的延迟超过10秒。这是我的服务代码: 'protected void onHandleIntent(Intent intent){ synchronized(this){ new StartChecking()。start((long)7000); } }' –
请确保'hostname url'是你的私有IP – samirk433
@ samir.k433这不是问题,连接工作,我也做了java服务器。问题是,只有当我使用我的PC模拟器时,连接才起作用,当我尝试在我的LG G3上启动它时,它只会在线程启动时崩溃。 也是我得到的问题:'跳过91帧!该应用程序可能在其主线程上做了太多工作。“在日志中,所以我认为我的程序效率不够高,而且为手机运行起来很沉重,而我只是不知道如何使它工作并且更高效。 –