in application have been using defaulthttpclient uploading images server takes time give response when server down. have put code here. , afik defaulthttpclient going deprecated, want use okhttp instead of defaulthttpclient. please me change code use okhttp uploading images. have searched lot don't know how make changes. please me....
public string uploadimg(byte[] image,string name,string phone,string imei,string randomid){ string mresponsedata = null; try{ mhttpclient = new defaulthttpclient(); mhttppost = new httppost(constants.base_url +"user/asset"); mhttppost.setheader("accept", "application/json"); mhttppost.setheader("mobile-number", phone); mhttppost.setheader("uid", imei); multipartentity reqentity = new multipartentity( httpmultipartmode.browser_compatible); if(image !=null) { bytearraybody bab = new bytearraybody(image, name); reqentity.addpart("file", bab); } mhttppost.setentity(reqentity); log.e("tag", "***** mhttpclient going execute "); httpparams httpparameters = mhttpclient.getparams(); httpconnectionparams.setconnectiontimeout(httpparameters, 2 * 1000); httpconnectionparams.setsotimeout(httpparameters, 2 * 1000); mhttpresponse = mhttpclient.execute(mhttppost); httpentity resentity = null; if(mhttpresponse!=null) resentity = mhttpresponse.getentity(); if(resentity!=null) mresponsedata = entityutils.tostring(resentity).trim(); }catch(httphostconnectexception e){ log.e("tag","exception cannot connect server while sending images ",e); return null; }catch(exception e){ log.e("tag","exception occured while sending images ",e); return null; } return mresponsedata; }
i had overcame issue replacing defaulthttpclient okhttpclient. far know defaulthttpclient deprecated in latest android versions.so had switched okhttpclient , issues fixed
Comments
Post a Comment