版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】
挑戰(zhàn)獨(dú)立開發(fā)項(xiàng)目能力--IT藍(lán)豹
做了5年的android開發(fā),今天沒事寫寫剛?cè)胄胁痪玫臅r(shí)候第一次獨(dú)立開發(fā)項(xiàng)目的心得體會(huì),
當(dāng)時(shí)我剛工作8個(gè)月,由于公司運(yùn)營不善倒閉了,在2011年3月份我開始準(zhǔn)備跳槽,
看了一周android筆試題和面試題后,然后就去找工作,當(dāng)時(shí)去面試的時(shí)候說自己有獨(dú)立項(xiàng)目開發(fā)的經(jīng)驗(yàn)。
結(jié)果面上了幾家公司,后來選擇一家游戲公司,開始游戲開發(fā)的生涯。當(dāng)時(shí)我去的時(shí)候就android組就我一個(gè)人,
也沒有人帶領(lǐng),去公司一個(gè)月后公司決定要我把網(wǎng)游游戲移植到手游,那時(shí)候確實(shí)沒有獨(dú)立開發(fā)項(xiàng)目的經(jīng)驗(yàn)。
但是又只有一個(gè)人做,沒辦法只有自己慢慢研究,那時(shí)候沒有像現(xiàn)在資源多,網(wǎng)上隨便找。
還是堅(jiān)持慢慢從網(wǎng)絡(luò)框架一步一步開始搭建。當(dāng)時(shí)獨(dú)立做完項(xiàng)目后感覺個(gè)人技術(shù)能力瞬間提高了很多,因?yàn)椴辉谶€怕獨(dú)立承擔(dān)項(xiàng)目了。
希望我的感受能給同樣剛?cè)胄械呐笥涯芙o與幫助。如今本人自己做一個(gè)技術(shù)網(wǎng)站:IT藍(lán)豹()
當(dāng)時(shí)使用的網(wǎng)絡(luò)框架是HttpClient,先實(shí)現(xiàn)ReceiveDataListener接口用來接收數(shù)據(jù)返回,然后設(shè)置如下代碼調(diào)用
調(diào)用方法:
privatevoidinitHttpClient(){
if(mHttpClient==null){
mHttpClient=HttpClientFactory
.newInstance(Constants.CONNECTION_TIMEOUT);
mHttpTransport=newHttpTransport(mHttpClient);
mHttpTransport.setReceiveDataListener(this);
}
}
網(wǎng)絡(luò)請(qǐng)求代碼部分如下:HttpClientFactory類
importjava.io.InputStream;importjava.security.KeyManagementException;importjava.security.KeyStore;importjava.security.KeyStoreException;importjava.security.NoSuchAlgorithmException;importjava.security.UnrecoverableKeyException;importorg.apache.http.HttpVersion;importorg.apache.http.client.HttpClient;importorg.apache.http.conn.params.ConnManagerParams;importorg.apache.http.conn.scheme.PlainSocketFactory;importorg.apache.http.conn.scheme.Scheme;importorg.apache.http.conn.scheme.SchemeRegistry;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;importorg.apache.http.params.BasicHttpParams;importorg.apache.http.params.HttpConnectionParams;importorg.apache.http.params.HttpParams;importorg.apache.http.params.HttpProtocolParams;/**
*Afactoryclasstoobtainasetup{@linkHttpClient}instancetobeused
*amongmultiplethreads.Thisinstanceshouldbekeptopenthroughoutthe
*applicationslifetimeandmustonlybeclosediftheclientisnotneeded
*anymore.
*<p>
*Thisfactorywillneverreturnasingletonsuchthateachcallto
*{@linkHttpClientFactory#newInstance(int,KeyStore)}resultsinanew
*instance.Commonlyonlyonecalltothismethodisneededforanapplicaiton.
*</p>
*/publicclassHttpClientFactory{
/**
*CreatesanewHttpClientinstancesetupwiththegiven{@linkKeyStore}.
*Theinstanceusesamulti-threadedconnectionmanagerwitha
*max-connectionsizesetto10.Aconnectionisreleasedbacktothepool
*oncetheresponse{@linkInputStream}isclosedorreaduntilEOF.
*<p>
*<b>Note:</b>AndroiddoesnotsupporttheJKSkeystoreprovider.For
*androidBKSshouldbeused.See<ahref="/">
*BouncyCastle</a>fordetails.
*</p>
*
*@paramaConnectionTimeout
*
theconnectiontimeoutforthis{@linkHttpClient}
*@paramaKeyStore
*
acryptographickeystorecontainingCA-Certificatesfor
*
trustedhosts.
*@returnanew{@linkHttpClient}instance.
*@throwsKeyManagementException
*
Ifthe{@linkKeyStore}initializationthrowsanexception
*@throwsNoSuchAlgorithmException
*
ifsslsocketfactorydoesnotsupportthekeystores
*
algorithm
*@throwsKeyStoreException
*
ifthe{@linkKeyStore}cannotbeopened.
*@throwsUnrecoverableKeyException
*
Ihavenotideawhenthishappens:)
*/
publicstaticHttpClientnewInstance(intaConnectionTimeout/*
*,KeyStore
*aKeyStore
*/)
/*
*throwsKeyManagementException,NoSuchAlgorithmException,
*KeyStoreException,UnrecoverableKeyException
*/{
finalHttpParamsparams=newBasicHttpParams();
HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params,"UTF-8");
HttpConnectionParams.setConnectionTimeout(params,aConnectionTimeout);
HttpConnectionParams.setTcpNoDelay(params,false);
//finalSSLSocketFactorysocketFactory=new
//SSLSocketFactory(aKeyStore);
//socketFactory
//.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
finalSchemeRegistryregistry=newSchemeRegistry();
registry.register(newScheme("http",newPlainSocketFactory(),80));
//registry.register(newScheme("https",socketFactory,443));
//connectionpoolislimitedto20connections
ConnManagerParams.setMaxTotalConnections(params,20);
//ServoConnPerRoutelimitsconnectionsperroute/host-currently
//thisisaconstant
ConnManagerParams.setMaxConnectionsPerRoute(params,
newAndhatConnPerRoute());
finalDefaultHttpClientdefaultHttpClient=newDefaultHttpClient(
newThreadSafeClientConnManager(params,registry),params);
returndefaultHttpClient;
}}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
HttpTransport類代碼如下:packagecom.andhat.android.http;importjava.io.ByteArrayOutputStream;importjava.io.Closeable;importjava.io.IOException;importjava.io.InputStream;import.URI;importjava.util.List;importorg.apache.http.HttpHost;importorg.apache.http.HttpRequest;importorg.apache.http.HttpResponse;importorg.apache.http.HttpStatus;importorg.apache.http.NameValuePair;importorg.apache.http.StatusLine;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.client.HttpClient;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.conn.params.ConnRoutePNames;importtocol.HTTP;importcom.andhat.android.utils.Constants;importcom.andhat.android.utils.UserPreference;importcom.andhat.android.utils.Utils;importandroid.content.Context;importandroid.os.Handler;importandroid.util.Log;publicclassHttpTransport{
privatefinalHttpClient_client;
privateReceiveDataListenermRecListener;
publicfinalstaticintRECEIVE_DATA_MIME_STRING=0;
publicfinalstaticintRECEIVE_DATA_MIME_PICTURE=1;
publicfinalstaticintRECEIVE_DATA_MIME_AUDIO=2;
publicHttpTransport(HttpClientaClient){
_client=aClient;
}
/**
*關(guān)閉連接
*/
publicvoidshutdown(){
if(_client!=null&&_client.getConnectionManager()!=null){
_client.getConnectionManager().shutdown();
}
}
publicvoidpost(booleanproxy,Handlerhandler,Stringurl,
List<NameValuePair>params,intmime)
throwsClientProtocolException,IOException{
if(proxy){
postByCMCCProxy(url,handler,params,mime);
}else{
post(URI.create(url),handler,params,mime);
}
}
privatevoidpostByCMCCProxy(Stringurl,Handlerhandler,
List<NameValuePair>params,intmime)
throwsClientProtocolException,IOException{
Stringhost=getHostByUrl(url);
Stringport=getPortByUrl(url);
HttpHostproxy=newHttpHost("72",80,"http");
HttpHosttarget=newHttpHost(host,Integer.parseInt(port),"http");
_client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
HttpPostpost=newHttpPost(url);
if(params!=null&¶ms.size()>0){
post.setEntity(newUrlEncodedFormEntity(params,HTTP.UTF_8));
}
HttpResponsehttpResponse=_client.execute(target,post);
StatusLinestatusLine=httpResponse.getStatusLine();
finalintstatus=statusLine.getStatusCode();
if(status!=HttpStatus.SC_OK){
Stringmessage="HTTPResponsecode:"
+statusLine.getStatusCode()+"-"
+statusLine.getReasonPhrase();
Log.e("connectmsg",message);
thrownewIOException(message);
}
InputStreamin=httpResponse.getEntity().getContent();
longlength=httpResponse.getEntity().getContentLength();
byte[]data=receiveData(null,url,in,length);
if(mRecListener!=null){
mRecListener.receive(data,mime);
}
handler.sendEmptyMessage(Constants.MSG_THAN_FILE);
post.abort();
}
//54:8080/AnimeInterface/Interface.do
privatevoidpost(URIuri,Handlerhandler,List<NameValuePair>params,
intmime)throwsClientProtocolException,IOException{
Log.e("Goo","HttpTransportpost()");
//try{
HttpPostpost=newHttpPost(uri);
if(params!=null&¶ms.size()>0){
post.setEntity(newUrlEncodedFormEntity(params,HTTP.UTF_8));
}
HttpResponsehttpResponse=_client.execute(post);
StatusLinestatusLine=httpResponse.getStatusLine();
finalintstatus=statusLine.getStatusCode();
if(status!=HttpStatus.SC_OK){
Stringmessage="HTTPResponsecode:"
+statusLine.getStatusCode()+"-"
+statusLine.getReasonPhrase();
shutdown();
thrownewIOException(message);
}
InputStreamin=httpResponse.getEntity().getContent();
longlength=httpResponse.getEntity().getContentLength();
byte[]data=receiveData(null,uri.toString(),in,length);
if(mRecListener!=null){
mRecListener.receive(data,mime);
}
handler.sendEmptyMessage(Constants.MSG_THAN_FILE);
post.abort();
//}catch(IllegalStateExceptione){
//Log.e("Goo","程序異常");
//shutdown();
//e.printStackTrace();
//}
}
publicvoidget(booleanproxy,Contextcontext,Handlerhanlder,
Stringurl,intmime)throwsIOException{
if(proxy){
getByCMCCProxy(url,context,hanlder,mime);
}else{
get(URI.create(url),context,hanlder,mime);
}
UserPreference.ensureIntializePreference(context);
}
privateStringgetPortByUrl(Stringurl){
intstart=url.indexOf(":");
if(start<0)
return"80";
ints=url.indexOf(":",start+1);
if(s<0)
return"80";
inte=url.indexOf("/",s+1);
if(e<0){
returnurl.substring(s+1);
}else{
returnurl.substring(s+1,e);
}
}
privateStringgetHostByUrl(Stringurl){
intstart=url.indexOf(":");
if(start<0)
returnnull;
ints=url.indexOf(":",start+1);
if(s>=0)
returnurl.substring(0,s);
inte=url.indexOf("/",start+3);
if(e>=0){
returnurl.substring(0,e);
}else{
returnurl;
}
}
privatevoidgetByCMCCProxy(Stringurl,Contextcontext,Handlerhandler,
intmime)throwsClientProtocolException,IOException{
Log.e("requesturl",url);
//try{
Stringhost=getHostByUrl(url);
Stringport=getPortByUrl(url);
HttpHostproxy=newHttpHost("72",80,"http");
HttpHosttarget=newHttpHost(host,Integer.parseInt(port),"http");
_client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
HttpGetget=newHttpGet(url);
HttpResponsehttpResponse=_client.execute(target,get);
StatusLinestatusLine=httpResponse.getStatusLine();
finalintstatus=statusLine.getStatusCode();
Log.e("getByCMCCProxystatus",""+status);
if(status!=HttpStatus.SC_OK){
Stringmessage="HTTPResponsecode:"
+statusLine.getStatusCode()+"-"
+statusLine.getReasonPhrase();
Log.e("getByCMCCProxy",message);
thrownewIOException(message);
}
InputStreamin=httpResponse.getEntity().getContent();
longlength=httpResponse.getEntity().getContentLength();
Log.e("Goo","GetRequestEntityLength:"+String.valueOf(length));
byte[]data=receiveData(context,url.toString(),in,length);
Log.e("Goo","datalength:"+String.valueOf(data.length));
if(mRecListener!=null&&length==data.length){
mRecListener.receive(data,mime);
}
handler.sendEmptyMessage(Constants.MSG_THAN_FILE);
get.abort();
//}catch(Exceptione){
//shutdown();
//e.printStackTrace();
//}
}
privatevoidget(URIuri,Contextcontext,Handlerhandler,intmime)
throwsIOException{
//try{
finalHttpGetget=newHttpGet(uri);
HttpResponsehttpResponse=_client.execute(get);
StatusLinestatusLine=httpResponse.getStatusLine();
finalintstatus=statusLine.getStatusCode();
if(status!=HttpStatus.SC_OK){
Stringmessage="HTTPResponsecode:"
+statusLine.getStatusCode()+"-"
+statusLine.getReasonPhrase();
thrownewIOException(message);
}
InputStreamin=httpResponse.getEntity().getContent();
longdownloadLength=httpResponse.getEntity().getContentLength();
byte[]data=receiveData(context,uri.toString(),in,downloadLength);
if(mRecListener!=null){
mRecListener.receive(data,mime);
}
handler.sendEmptyMessage(Constants.MSG_THAN_FILE);
get.abort();
//}catch(IllegalStateExceptione){
//shutdown();
//e.printStackTrace();
//}
}
publicbyte[]receiveData(Contextcontext,Stringurl,InputStreamin,longlength)
throwsIOException{
ByteArrayOutputStreambos=null;
intdownloadSize=0;
byte[]retval=null;
Stringstr=url;
String[]s=str.split("/");
for(Strings1:s){
if(s1.contains(".")&&s1.endsWith(".gif")
||s1.endsWith(".amr")){
saveFailFile(context,s1);
Log.e("Goo_downLoadSize","fileName:"+s1);
}
}
try{
bos=newByteArrayOutputStream();
byte[]buf=newbyte[1024];
intlen=0;
while((len=in.read(buf))!=-1){
bos.write(buf,0,len);
downloadSize=len+downloadSize;
}
Log.e("Goo_downLoadSize","downloadSize:"+downloadSize+"");
Log.e("Goo_downLoadSize","length:"+length+"");
if(downloadSize==length&&downloadSize!=-1){
saveFailFile(context,null);
}
retval=bos.toByteArray();
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度全款購入進(jìn)口跑車合同范本3篇
- 二零二五年度企業(yè)與個(gè)人投資回報(bào)對(duì)賭協(xié)議3篇
- 二零二五年度員工試用期工作內(nèi)容調(diào)整及考核標(biāo)準(zhǔn)協(xié)議3篇
- 二零二五年度電商平臺(tái)商家會(huì)員返利合同3篇
- 2025年度新能源汽車產(chǎn)業(yè)鏈投資基金合作協(xié)議3篇
- 2025年度公司股東內(nèi)部關(guān)于企業(yè)并購整合的專項(xiàng)協(xié)議3篇
- 2025年度綠色能源項(xiàng)目分?jǐn)倕f(xié)議3篇
- 二零二五年度新能源汽車充電樁建設(shè)投資入股合同3篇
- 2025通信銷售合同
- 2025年農(nóng)村土地永久轉(zhuǎn)讓與農(nóng)村電商合作框架合同3篇
- 承壓設(shè)備事故及處理課件
- 煤層氣現(xiàn)場(chǎng)監(jiān)督工作要點(diǎn)
- 工會(huì)經(jīng)費(fèi)收支預(yù)算表
- 舒爾特方格55格200張?zhí)岣邔W⒘4紙直接打印版
- 質(zhì)量管理體系各條款的審核重點(diǎn)
- 聚丙烯化學(xué)品安全技術(shù)說明書(MSDS)
- BBC美麗中國英文字幕
- 衛(wèi)生院工程施工組織設(shè)計(jì)方案
- CDR-臨床癡呆評(píng)定量表
- 《八年級(jí)下學(xué)期語文教學(xué)個(gè)人工作總結(jié)》
- 鋁合金門窗制作工藝卡片 - 修改
評(píng)論
0/150
提交評(píng)論