Android:SocketTimeoutException: failed to connect to /103.24.4.60 (port 80) after 30000ms -
i want download image http url , save sd card after load , show image view. have tried image-loader tutorial android-hive used http url load image after run image can't load , show. app not crash getting error exception java.net.sockettimeoutexception: failed connect /103.24.4.60 (port 80) after 30000ms
in imageloader class
here log information
09-30 02:32:15.820 18371-18389/? w/system.err﹕ java.net.sockettimeoutexception: failed connect /103.24.4.60 (port 80) after 30000ms 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ libcore.io.iobridge.connecterrno(iobridge.java:169) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ libcore.io.iobridge.connect(iobridge.java:122) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ java.net.plainsocketimpl.connect(plainsocketimpl.java:183) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ java.net.plainsocketimpl.connect(plainsocketimpl.java:456) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ java.net.socket.connect(socket.java:882) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ com.android.okhttp.internal.platform.connectsocket(platform.java:139) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ com.android.okhttp.connection.connect(connection.java:148) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ com.android.okhttp.internal.http.httpengine.connect(httpengine.java:276) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ com.android.okhttp.internal.http.httpengine.sendrequest(httpengine.java:211) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ com.android.okhttp.internal.http.httpurlconnectionimpl.execute(httpurlconnectionimpl.java:373) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ com.android.okhttp.internal.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:323) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ com.android.okhttp.internal.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:190) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ com.example.tazeen.imgloader.imageloader.getbitmap(imageloader.java:73) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ com.example.tazeen.imgloader.imageloader.access$000(imageloader.java:23) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ com.example.tazeen.imgloader.imageloader$photosloader.run(imageloader.java:134) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ java.util.concurrent.executors$runnableadapter.call(executors.java:422) 09-30 02:32:15.820 18371-18389/? w/system.err﹕ @ java.util.concurrent.futuretask.run(futuretask.java:237) 09-30 02:32:15.821 18371-18389/? w/system.err﹕ @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 09-30 02:32:15.821 18371-18389/? w/system.err﹕ @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 09-30 02:32:15.821 18371-18389/? w/system.err﹕ @ java.lang.thread.run(thread.java:818)
this imageloader class
public class imageloader { memorycache memorycache=new memorycache(); filecache filecache; private map<imageview, string> imageviews=collections.synchronizedmap(new weakhashmap<imageview, string>()); executorservice executorservice; public imageloader(context context){ filecache=new filecache(context); executorservice=executors.newfixedthreadpool(5); } int stub_id = r.drawable.ic_launcher; public void displayimage(string url, int loader, imageview imageview) { stub_id = loader; imageviews.put(imageview, url); bitmap bitmap=memorycache.get(url); if(bitmap!=null) imageview.setimagebitmap(bitmap); else { queuephoto(url, imageview); imageview.setimageresource(loader); } } private void queuephoto(string url, imageview imageview) { phototoload p=new phototoload(url, imageview); executorservice.submit(new photosloader(p)); } private bitmap getbitmap(string url) { file f=filecache.getfile(url); //from sd cache bitmap b = decodefile(f); if(b!=null) return b; //from web try { bitmap bitmap=null; url imageurl = new url(url); httpurlconnection conn = (httpurlconnection)imageurl.openconnection(); conn.setconnecttimeout(30000); conn.setreadtimeout(30000); conn.setinstancefollowredirects(true); inputstream is=conn.getinputstream(); outputstream os = new fileoutputstream(f); utils.copystream(is, os); os.close(); bitmap = decodefile(f); return bitmap; } catch (exception ex){ ex.printstacktrace(); return null; } } //decodes image , scales reduce memory consumption private bitmap decodefile(file f){ try { //decode image size bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; bitmapfactory.decodestream(new fileinputstream(f),null,o); //find correct scale value. should power of 2. final int required_size=70; int width_tmp=o.outwidth, height_tmp=o.outheight; int scale=1; while(true){ if(width_tmp/2<required_size || height_tmp/2<required_size) break; width_tmp/=2; height_tmp/=2; scale*=2; } //decode insamplesize bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize=scale; return bitmapfactory.decodestream(new fileinputstream(f), null, o2); } catch (filenotfoundexception e) {} return null; } //task queue private class phototoload { public string url; public imageview imageview; public phototoload(string u, imageview i){ url=u; imageview=i; } } class photosloader implements runnable { phototoload phototoload; photosloader(phototoload phototoload){ this.phototoload=phototoload; } @override public void run() { if(imageviewreused(phototoload)) return; bitmap bmp=getbitmap(phototoload.url); memorycache.put(phototoload.url, bmp); if(imageviewreused(phototoload)) return; bitmapdisplayer bd=new bitmapdisplayer(bmp, phototoload); activity a=(activity)phototoload.imageview.getcontext(); a.runonuithread(bd); } } boolean imageviewreused(phototoload phototoload){ string tag=imageviews.get(phototoload.imageview); if(tag==null || !tag.equals(phototoload.url)) return true; return false; } //used display bitmap in ui thread class bitmapdisplayer implements runnable { bitmap bitmap; phototoload phototoload; public bitmapdisplayer(bitmap b, phototoload p){bitmap=b;phototoload=p;} public void run() { if(imageviewreused(phototoload)) return; if(bitmap!=null) phototoload.imageview.setimagebitmap(bitmap); else phototoload.imageview.setimageresource(stub_id); } } public void clearcache() { memorycache.clear(); filecache.clear(); } }
there 2 possibilities: 1)have checked , tested connection.
2)better don't set connection timeout,if setting chose maximum time,,because throws error,if server didn't response within given time.
so can use
httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setconnecttimeout(7000);
or can use
for (int retries = 0; retries < 3; retries++) { try { final httpclient client = createhttpclientwithdefaultsocketfactory(null, null); final httpresponse response = client.execute(get); final int statuscode = response.getstatusline().getstatuscode(); if (statuscode != 200) { throw new illegalstateexception("get request on '" + get.geturi().tostring() + "' resulted in " + statuscode); } else { return response.getentity(); } } catch (final java.net.sockettimeoutexception e) { // connection timed out...let's try again } }
hope helps you, enjoy code:)
Comments
Post a Comment