1、 if (fos != null) try fos.flush(); fos.close(); catch (IOException e) / TODO: handle exception 2.下载图片工具publicclassDownloadPicUtil /*paramurl*paramfilename*return0没有图片;return1成功;return2IO异常;*/ publicstaticintDownLoadBitmap(String url,Stringfilename) / TODO Auto-generated method stub InputStream input
2、Stream= null; DefaultHttpClient client = newDefaultHttpClient(); HttpGet request=newHttpGet(url); HttpResponse response=client.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if(statusCode!=HttpStatus.SC_OK) return 0; else HttpEntity entity =response.getEntity(); inputSt
3、ream = entity.getContent(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); /图片可以在这个时候存储到sdcard上 saveBitmap(bitmap,filename); return 1; catch (Exception e) return 2; finally if(inputStream!=null) inputStream.close(); / TODO Auto-generated catch block e.printStackTrace(); publicstaticvoidsav
4、eBitmap(Bitmap bitName,String filename) String filepath = /sdcard/baomi/; File f = newFile(filepath+filename); FileOutputStream fout = null; try f.createNewFile(); fout = newFileOutputStream(f); bitNpress(Bitmap.CompressFormat.PNG,100,fout); fout.flush(); fout.close(); catch(Exception e)3.http请求工具pu
5、blicclassHttpUtil publicstaticStringSESSION_ID=null; publicstaticHttpResponsegetHttpResponse(HttpGet request) throws ClientProtocolException,IOException BasicHttpParams httpParams = newBasicHttpParams(); LogService.getInstance().recordLog(进入getHttpResponse); HttpConnectionParams.setConnectionTimeout
6、(httpParams, SysConfig.nConnectionTimeout);超时时间为:+SysConfig.nConnectionTimeout); HttpConnectionParams.setSoTimeout(httpParams, SysConfig.nSoTimeout);Socket超时时间为:+SysConfig.nSoTimeout); DefaultHttpClient httpClient = newDefaultHttpClient(httpParams);获得响应客户端 HttpResponse response=httpClient.execute(re
7、quest);获得结果 httpClient.getConnectionManager().shutdown();关闭连接 return response; publicstaticHttpResponsegetHttpResponse(HttpPost request) throws ClientProtocolException,IOException /通过url发送post请求,并返回结果 publicstaticStringqueryStringForPost(String url)发送POST请求 HttpPost request = newHttpPost(url); Strin
8、g result=null; HttpResponse response=HttpUtil.getHttpResponse(request); LogService.getInstance().recordLog(POST返回状态为+response.getStatusLine().getStatusCode(); if(response.getStatusLine().getStatusCode()=200) result=EntityUtils.toString(response.getEntity(); return result; catch (Exception e) POST异常:
9、+e.getMessage(); e.printStackTrace(); result=网络异常return result; returnnull; /通过url发送GET请求并返回结果 publicstaticStringqueryStringForGet(String url)发送GET请求 ResponseHandler responseHandler = newBasicResponseHandler(); HttpClient httpclient = newDefaultHttpClient();提交的地址为:+url); HttpGet httpget = newHttpGet
10、(url); result = httpclient.execute(httpget, responseHandler);GET返回状态为+result); return result;GET异常: /通过HttpPost发送POST请求,并返回结果 publicstaticStringqueryStringForPost(HttpPost request)4.JSon工具public class JSonUtil public static String string2json(String s) StringBuilder sb = new StringBuilder(s.length()
11、+20); sb.append( for (int i=0;is.length();i+) char c =s.charAt(i); switch(c) case : sb.append( break;/bbffnnrrtt default: sb.append(c); return sb.toString(); public static String number2json(Number number) return number.toString(); public static String boolean2json(Boolean bool) return bool.toString
12、(); public static String map2json(Map map) if(map.isEmpty() return StringBuilder sb = new StringBuilder(map.size()4); sb.append( Set keys = map.keySet(); for(String key :keys) Object value = map.get(key); sb.append( sb.append(key); sb.append( sb.append(tojson(value);, sb.setCharAt(sb.length()-1, pub
13、lic static String array2json(Object array) if(array.length=0) StringBuilder sb = new StringBuilder(array.length for(Object o:array) sb.append(tojson(o); public static String tojson(Object o) if(o=null)null if(o instanceof String) return string2json(String)o); if(o instanceof Boolean) return boolean2
14、json(Boolean)o); if(o instanceof Number) return number2json(Number)o); if(o instanceof Map) return map2json(Map)o); if(o instanceof Object) return array2json(Object)o); throw new RuntimeException(Unsupported type :+o.getClass().getName(); /*Servlet 输出json response.setContentType(application/json;cha
15、rset=UTF-8 response.setCharacterEncoding(UTF-8 PrintWriter pw = response.getWriter(); pw.write(JSonUtil.tojson(obj); pw.flush(); */5.Stirng encode 转ucs2码双字节publicclassStringUtil publicstaticStringStringToHex(String input) String result = if (input ! byte bytes = null; try bytes = input.getBytes(UTF-
16、16BE catch (Exception e) / TODO Auto-generated catch block returnnull; StringBuffer reValue = newStringBuffer(); StringBuffer tem = newStringBuffer(); for (int i = 0; i bytes.length; i+) tem.delete(0, tem.length(); tem.append(Integer.toHexString(bytesi & 0xFF); if (tem.length() = 1) tem.insert(0, 0
17、reValue.append(tem); result = reValue.toString().toUpperCase(); else return publicstaticStringHexToUTF16BE(String input) Stringresult = byte bytes1 = newbyteinput.length() / 2; input.length(); i += 2) bytes1i / 2 = (byte) (Integer.parseInt(input.substring(i, i + 2), 16); result = newString(bytes1, c
18、atch (UnsupportedEncodingException e) return null; return 6.Notification实例NotificationManager notificationManager=(NotificationManager)context.getSystemService(NOTIFICATION_SERVICE); /定义一个Notification Notification m_notification = newNotification(); /设置Notification的各种属性 /设置通知在状态栏的图标 m_notification.i
19、con = R.drawable.icon; /当我们点击通知时显示的内容 m_notification.tickerText = BUTTON1通知内容 /通知时发出的默认声音 m_notification.defaults = Notification.DEFAULT_SOUND; /设置通知显示的参数 Intent intent = newIntent(); PendingIntent m_pendingIntent = PendingIntent.getActivity(testNotification.this,0,intent,0); m_notification.setLatestEventInfo(testNotification.this,BUTTON,m_pendingIntent); /开始执行通知 i+; notificationManager.notify(i,m_notification);7.POSTURL TheardpublicclassPostThreadextendsThread privatestaticContextcontex
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1