Android 运行 HTTP post/get 方法时出现滞后/停顿
因此,我有一个成功的应用程序,其中包含一个在我的网站上注册用户的表单,并且我创建了一个 15 帧 png 动画,该动画也可以在命令下运行良好。
我首先启动动画(并循环),然后在动画结束时运行 HTTP POST。当 HTTP Post 执行其操作时,动画(几乎所有 Android)都会滞后或暂停,然后在 POST 完成后继续运行。
这是正常的吗?有没有办法让它在运行 POST 时不滞后?
谢谢!
对于那些好奇的人,这是我的 httpClass(mywebsite.com 只是我实际 URL 的一个支柱)
try{
Log.d("MYTAG", "Registration begin");
HttpClient client = new DefaultHttpClient();
String postURL = "mywebsite.com";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("fullName", fullName));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if(resEntity!=null){
newCode = EntityUtils.toString(resEntity);
} else {
newCode = (String) null;
}
}catch(Exception e){
Log.d("MYTAG", "Exception e="+e);
}
return newCode;
}
So I have a successful application with a form that registers a user with my website, and I created a 15 frame png animation that also runs well on command.
I have the animation starts up (and is looping) first and then run the HTTP POST at the end of the animation. When the HTTP Post is doing its thing, the animation (pretty much all of android) lags or pauses and then will continue to function after the POST is done.
Is this normal? Is there a way to make it not lag when running the POST?
Thanks!
And for those who are curious, here's my httpClass (mywebsite.com is just a prop for my actual URL)
try{
Log.d("MYTAG", "Registration begin");
HttpClient client = new DefaultHttpClient();
String postURL = "mywebsite.com";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("fullName", fullName));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if(resEntity!=null){
newCode = EntityUtils.toString(resEntity);
} else {
newCode = (String) null;
}
}catch(Exception e){
Log.d("MYTAG", "Exception e="+e);
}
return newCode;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 AsyncTask 来解决这个问题。 Google 在此处对此进行了介绍。它不会让事情进展得更快,但会防止你的 UI 线程停滞。
You can fix that up with an AsyncTask. Google has an introduction to it here. It won't make things go faster but it will keep your UI thread from stalling out.
根据您的描述,我对您的问题的猜测是您在主线程上执行此代码,这会干扰动画的绘制。为了获得更多帮助,我认为我们需要看到更多的活动。对于此类问题,我们必须了解您正在使用的线程以及如何将网络代码移出主应用程序线程。
Based on your description, my guess at your problem would be that you're executing this code on the main thread which is interfering with the drawing of the animation. To be more help, I think we'd need to see more of the Activity. For these sorts of issues, we'd have to know the threads you are using and how you are moving your net code off your main application thread.
您是否在自己的线程中运行此代码?如果你不这样做,那么它就会滞后/停止,因为 UI 线程被阻塞。
Are you running this code in it's own thread? If you aren't then it's lags/stops because the UI thread is being blocked.