版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】
挑戰(zhàn)獨立開發(fā)項目能力--IT藍(lán)豹
做了5年的android開發(fā),今天沒事寫寫剛?cè)胄胁痪玫臅r候第一次獨立開發(fā)項目的心得體會,
當(dāng)時我剛工作8個月,由于公司運營不善倒閉了,在2011年3月份我開始準(zhǔn)備跳槽,
看了一周android筆試題和面試題后,然后就去找工作,當(dāng)時去面試的時候說自己有獨立項目開發(fā)的經(jīng)驗。
結(jié)果面上了幾家公司,后來選擇一家游戲公司,開始游戲開發(fā)的生涯。當(dāng)時我去的時候就android組就我一個人,
也沒有人帶領(lǐng),去公司一個月后公司決定要我把網(wǎng)游游戲移植到手游,那時候確實沒有獨立開發(fā)項目的經(jīng)驗。
但是又只有一個人做,沒辦法只有自己慢慢研究,那時候沒有像現(xiàn)在資源多,網(wǎng)上隨便找。
還是堅持慢慢從網(wǎng)絡(luò)框架一步一步開始搭建。當(dāng)時獨立做完項目后感覺個人技術(shù)能力瞬間提高了很多,因為不在還怕獨立承擔(dān)項目了。
希望我的感受能給同樣剛?cè)胄械呐笥涯芙o與幫助。如今本人自己做一個技術(shù)網(wǎng)站:IT藍(lán)豹()
當(dāng)時使用的網(wǎng)絡(luò)框架是HttpClient,先實現(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ò)請求代碼部分如下: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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 電機學(xué)課件-清華大學(xué)
- 2024年全新裝修設(shè)計合作協(xié)議2篇
- 廣西大學(xué)附屬中學(xué)消防講座課件張琳敏課件
- 房屋擔(dān)保租賃合同(2篇)
- 2024年互聯(lián)網(wǎng)租賃平臺自行車退租退款及押金返還協(xié)議3篇
- 2025年貴州貨運從業(yè)資格考試模擬考試題庫及答案解析
- 2025年福州貨運從業(yè)資格試題答案解析
- 2025年武漢貨運從業(yè)資格證考試模擬考試題及答案
- 2025年克拉瑪依b2考貨運資格證要多久
- 2025年塔城貨運資格證培訓(xùn)考試題
- 《俄羅斯國情概況》課件
- 湖南省長沙市六年級上冊數(shù)學(xué)期末試卷(含答案)
- 30題啟明星辰售前工程師崗位常見面試問題含HR問題考察點及參考回答
- 幕墻工程檢驗批質(zhì)量驗收記錄
- 2023年日本醫(yī)藥行業(yè)分析報告
- 關(guān)于社會保險經(jīng)辦機構(gòu)內(nèi)部控制講解
- 軟件開發(fā)項目關(guān)鍵技術(shù)可行性分析
- 虛擬貨幣交易所行業(yè)營銷方案
- 山東建筑大學(xué)混凝土結(jié)構(gòu)設(shè)計期末考試復(fù)習(xí)題
- 衛(wèi)生院防雷安全應(yīng)急預(yù)案
- 膠原蛋白注射知情同意書
評論
0/150
提交評論