android常用的代码 直接复制粘贴可用Word文档格式.docx

上传人:b****5 文档编号:20950634 上传时间:2023-01-26 格式:DOCX 页数:87 大小:117.47KB
下载 相关 举报
android常用的代码 直接复制粘贴可用Word文档格式.docx_第1页
第1页 / 共87页
android常用的代码 直接复制粘贴可用Word文档格式.docx_第2页
第2页 / 共87页
android常用的代码 直接复制粘贴可用Word文档格式.docx_第3页
第3页 / 共87页
android常用的代码 直接复制粘贴可用Word文档格式.docx_第4页
第4页 / 共87页
android常用的代码 直接复制粘贴可用Word文档格式.docx_第5页
第5页 / 共87页
点击查看更多>>
下载资源
资源描述

android常用的代码 直接复制粘贴可用Word文档格式.docx

《android常用的代码 直接复制粘贴可用Word文档格式.docx》由会员分享,可在线阅读,更多相关《android常用的代码 直接复制粘贴可用Word文档格式.docx(87页珍藏版)》请在冰豆网上搜索。

android常用的代码 直接复制粘贴可用Word文档格式.docx

if(fos!

=null){

try{

fos.flush();

fos.close();

}catch(IOExceptione){

//TODO:

handleexception

}

}

2.下载图片工具

publicclassDownloadPicUtil{

/**

*

*@paramurl

*@paramfilename

*@return0没有图片;

return1成功;

return2IO异常;

*/

publicstaticintDownLoadBitmap(Stringurl,Stringfilename){

//TODOAuto-generatedmethodstub

InputStreaminputStream=null;

DefaultHttpClientclient=newDefaultHttpClient();

HttpGetrequest=newHttpGet(url);

HttpResponseresponse=client.execute(request);

intstatusCode=response.getStatusLine().getStatusCode();

if(statusCode!

=HttpStatus.SC_OK){

return0;

}else{

HttpEntityentity=response.getEntity();

inputStream=entity.getContent();

Bitmapbitmap=BitmapFactory.decodeStream(inputStream);

//图片可以在这个时候存储到sdcard上

saveBitmap(bitmap,filename);

return1;

}catch(Exceptione){

return2;

}finally{

if(inputStream!

=null){

inputStream.close();

//TODOAuto-generatedcatchblock

e.printStackTrace();

publicstaticvoidsaveBitmap(BitmapbitName,Stringfilename){

Stringfilepath="

/sdcard/baomi/"

;

Filef=newFile(filepath+filename);

FileOutputStreamfout=null;

try{

f.createNewFile();

fout=newFileOutputStream(f);

bitNpress(Bitmap.CompressFormat.PNG,100,fout);

fout.flush();

fout.close();

}catch(Exceptione){

}

3.http请求工具

publicclassHttpUtil{

publicstaticStringSESSION_ID=null;

publicstaticHttpResponsegetHttpResponse(HttpGetrequest)throwsClientProtocolException,IOException{

BasicHttpParamshttpParams=newBasicHttpParams();

LogService.getInstance().recordLog("

进入getHttpResponse"

);

HttpConnectionParams.setConnectionTimeout(httpParams,

SysConfig.nConnectionTimeout);

超时时间为:

"

+SysConfig.nConnectionTimeout);

HttpConnectionParams.setSoTimeout(httpParams,SysConfig.nSoTimeout);

Socket超时时间为:

+SysConfig.nSoTimeout);

DefaultHttpClienthttpClient=newDefaultHttpClient(httpParams);

获得响应客户端"

HttpResponseresponse=httpClient.execute(request);

获得结果"

httpClient.getConnectionManager().shutdown();

关闭连接"

returnresponse;

publicstaticHttpResponsegetHttpResponse(HttpPostrequest)throwsClientProtocolException,IOException{

//通过url发送post请求,并返回结果

publicstaticStringqueryStringForPost(Stringurl){

发送POST请求"

HttpPostrequest=newHttpPost(url);

Stringresult=null;

HttpResponseresponse=HttpUtil.getHttpResponse(request);

LogService.getInstance().recordLog("

POST返回状态为"

+response.getStatusLine().getStatusCode());

if(response.getStatusLine().getStatusCode()==200){

result=EntityUtils.toString(response.getEntity());

returnresult;

}catch(Exceptione){

POST异常:

+e.getMessage());

e.printStackTrace();

result="

网络异常"

returnresult;

returnnull;

//通过url发送GET请求并返回结果

publicstaticStringqueryStringForGet(Stringurl){

发送GET请求"

ResponseHandler<

String>

responseHandler=newBasicResponseHandler();

HttpClienthttpclient=newDefaultHttpClient();

提交的地址为:

+url);

HttpGethttpget=newHttpGet(url);

result=httpclient.execute(httpget,responseHandler);

GET返回状态为"

+result);

returnresult;

GET异常:

//通过HttpPost发送POST请求,并返回结果

publicstaticStringqueryStringForPost(HttpPostrequest){

4.JSon工具

publicclassJSonUtil{

publicstaticStringstring2json(Strings){

StringBuildersb=newStringBuilder(s.length()+20);

sb.append('

\"

'

for(inti=0;

i<

s.length();

i++){

charc=s.charAt(i);

switch(c){

case'

:

sb.append("

\\\"

break;

\\'

\\\\"

/'

\\/"

\b'

\\b"

\f'

\\f"

\n'

\\n"

\r'

\\r"

\t'

\\t"

default:

sb.append(c);

returnsb.toString();

publicstaticStringnumber2json(Numbernumber){

returnnumber.toString();

publicstaticStringboolean2json(Booleanbool){

returnbool.toString();

publicstaticStringmap2json(Mapmap){

if(map.isEmpty()){

return"

[]"

StringBuildersb=newStringBuilder(map.size()<

<

4);

sb.append("

{"

Set<

keys=map.keySet();

for(Stringkey:

keys){

Objectvalue=map.get(key);

sb.append('

sb.append(key);

sb.append("

sb.append(tojson(value));

'

sb.setCharAt(sb.length()-1,'

}'

publicstaticStringarray2json(Object[]array){

if(array.length==0){

StringBuildersb=newStringBuilder(array.length<

["

for(Objecto:

array){

sb.append(tojson(o));

]'

publicstaticStringtojson(Objecto){

if(o==null)

null"

if(oinstanceofString)

returnstring2json((String)o);

if(oinstanceofBoolean)

returnboolean2json((Boolean)o);

if(oinstanceofNumber)

returnnumber2json((Number)o);

if(oinstanceofMap)

returnmap2json((Map)o);

if(oinstanceofObject[])

returnarray2json((Object[])o);

thrownewRuntimeException("

Unsupportedtype:

+o.getClass().getName());

/*Servlet输出json

response.setContentType("

application/json;

charset=UTF-8"

response.setCharacterEncoding("

UTF-8"

PrintWriterpw=response.getWriter();

pw.write(JSonUtil.tojson(obj));

pw.flush();

 

*/

5.Stirngencode转ucs2码双字节

publicclassStringUtil{

publicstaticStringStringToHex(Stringinput){

Stringresult="

if(input!

byte[]bytes=null;

try{

bytes=input.getBytes("

UTF-16BE"

}catch(Exceptione){

//TODOAuto-generatedcatchblock

returnnull;

StringBufferreValue=newStringBuffer();

StringBuffertem=newStringBuffer();

for(inti=0;

i<

bytes.length;

i++){

tem.delete(0,tem.length());

tem.append(Integer.toHexString(bytes[i]&

0xFF));

if(tem.length()==1){

tem.insert(0,'

0'

reValue.append(tem);

result=reValue.toString().toUpperCase();

}else{

return"

publicstaticStringHexToUTF16BE(Stringinput){

Stringresult="

byte[]bytes1=newbyte[input.length()/2];

input.length();

i+=2){

bytes1[i/2]=(byte)(Integer.parseInt(input.substring(i,

i+2),16));

result=newString(bytes1,"

}catch(UnsupportedEncodingExceptione){

returnnull;

return"

6.Notification实例

NotificationManagernotificationManager=(NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);

//定义一个Notification

Notificationm_notification=newNotification();

//设置Notification的各种属性

//设置通知在状态栏的图标

m_notification.icon=R.drawable.icon;

//当我们点击通知时显示的内容

m_notification.tickerText="

BUTTON1通知内容"

//通知时发出的默认声音

m_notification.defaults=Notification.DEFAULT_SOUND;

//设置通知显示的参数

Intentintent=newIntent();

PendingIntentm_pendingIntent=PendingIntent.getActivity(testNotification.this,0,intent,0);

m_notification.setLatestEventInfo(testNotification.this,"

BUTTON"

"

m_pendingIntent);

//开始执行通知

i++;

notificationManager.notify(i,m_notification);

7.POSTURLTheard

publicclassPostThreadextendsThread{

privatestaticContextcontex

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 广告传媒

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1