Android Set Network request timeout

Source: Internet
Author: User

UI interface updates must not operate in the UI thread in the Ruanable thread UI can send messages using handler to update the UI
private void Load () {
Loaddate load = new Loaddate ();
Load.execute ("http://h.hiphotos.baidu.com/image/w%3D310/sign=1bc9c0da38292df597c3aa148c305ce2/ C83d70cf3bc79f3d77fbe1c5b8a1cd11728b2928.jpg ");
}

Class Loaddate extends Asynctask<string, Integer, bitmap> {

@Override
protected void OnPreExecute () {

Mydialog.settitle ("Please Wait");
Mydialog.setmessage ("Loading ...");
Mydialog.setcanceledontouchoutside (FALSE);
Mydialog.show ();
}

@Override
Protected Bitmap doinbackground (String ... params) {
URL myfileurl = null;
Bitmap Bitmap = null;
InputStream is = null;
HttpURLConnection conn = null;


try {
Myfileurl = new URL (params[0]);
} catch (Malformedurlexception e) {
E.printstacktrace ();
}
try {
conn = (httpurlconnection) myfileurl
. OpenConnection ();
Conn.setdoinput (TRUE);
Conn.connect ();
Conn.setconnecttimeout (3000);
Conn.setreadtimeout (3000);
is =conn.getinputstream ();
Bitmap =bitmapfactory.decodestream (IS);
Is.close ();
} catch (IOException e) {
E.printstacktrace ();
}finally{
try {
if (is = null) {
Is.close ();
}
IF (conn! = null) {
Conn.disconnect ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
StartTime (bitmap);
return bitmap;

}

Handler myhandler=new Handler () {
public void Handlemessage (Message msg) {
if (msg.what==1) {
Toast.maketext (mainactivity.this, "Connection server timeout" +msg.obj, 1). Show ();
}
};
};
private void StartTime (final Bitmap BM) {
Runnable run=new Runnable () {
Boolean isrun=true;
Long Enabletime;
Long Starttime=system.currenttimemillis ();
@Override
public void Run () {
System.out.println (bm+ "---BM----");
while (Isrun) {
Enabletime=system.currenttimemillis ()-starttime;
if (enabletime>=3000 &&bm==null) {
Message message = new Message ();
Message.what = 1;
Message.obj= "Demo";
Myhandler.sendmessage (message);
Toast.maketext (mainactivity.this, "Connection Server timeout", 1). Show ();
Isrun=false;
}
try{
Thread.Sleep (50);
}catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Timer thread sleep Ex:" +e.tostring ());
}
}
SYSTEM.OUT.PRINTLN ("Timer thread run: End time: "+enabletime);
}
};
Looper.prepare ();

New Thread (Run). Start ();

}

@Override
protected void OnPostExecute (Bitmap result) {
Image.setimagebitmap (result);
Mydialog.cancel ();
System.out.println (result+ "---");
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.