我也是剛剛接觸程序開發(fā),一個菜鳥。前面和幾個同學準備做一個移動教務(wù)系統(tǒng),網(wǎng)上看了很多資料都說了對運行一些費時。如數(shù)據(jù)庫、網(wǎng)絡(luò)的鏈接操作,需要新開一個thread對其進行處理。但是后面在對程序進行調(diào)試的過程中,剛開始的時候報了空指針錯誤。根據(jù)錯誤提示,進行修改。說實話,經(jīng)過那個過程發(fā)現(xiàn)自己真的還很菜,排錯的經(jīng)驗太少了。直到過了好久才想到報了空指針錯誤,是因為在線程里生成的對象,因為有時間延遲,對于線程后面的對象來說是空的,所以才導(dǎo)致了空指針錯誤。后面參考的解決方法是利用join()函數(shù)。當然也可用其他方法。
在武義等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、做網(wǎng)站 網(wǎng)站設(shè)計制作專業(yè)公司,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計,全網(wǎng)整合營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),武義網(wǎng)站建設(shè)費用合理。下面寫的代碼:
public class MyThread extends Thread { private InputStream is = null; private String url; private String method; private Listparams; public MyThread(String url, String method, List params) { this.method = method; this.url = url; this.params = params; } public void run() { try { // check for request method if (method.equals("POST")) { // request method is POST // defaultHttpClient BasicHttpParams httpParameters = new BasicHttpParams(); // Set the default socket timeout (SO_TIMEOUT) HttpConnectionParams .setConnectionTimeout(httpParameters, 30000); // in milliseconds which is the timeout for waiting for // data. HttpConnectionParams.setSoTimeout(httpParameters, 30000); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); setIs(httpEntity.getContent()); } else if (method.equals("GET")) { BasicHttpParams httpParameters = new BasicHttpParams(); // Set the default socket timeout (SO_TIMEOUT) HttpConnectionParams .setConnectionTimeout(httpParameters, 30000); // in milliseconds which is the timeout for waiting for // data. HttpConnectionParams.setSoTimeout(httpParameters, 30000); // request method is GET DefaultHttpClient httpClient = new DefaultHttpClient(); String paramString = URLEncodedUtils.format(params, "utf-8"); String temp_url = url + "?" + paramString; HttpGet httpGet = new HttpGet(temp_url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); setIs(httpEntity.getContent()); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public InputStream getIs() { return is; } public void setIs(InputStream is) { this.is = is; } }
通過下面的語句對以上生成的對象(“setIs(httpEntity.getContent());
“)進行調(diào)用
MyThread myThread = new MyThread(url, method, params); myThread.start(); myThread.join();//同做join()函數(shù)對myThread進行處理,使其一般的對象那樣使用即可 is = myThread.getIs();//獲得is對象
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。