Android lags/stalls when running the HTTP post/get methods
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 afte开发者_开发技巧r 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;
}
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.
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.
精彩评论