文章標題

Android學習之——訪問服務器

實現技術:通過HttpClient來訪問服務器

功能:實現把指定參數通過post請求發送到服務端,服務端通過HttpServletRequest取出Post當中的參數進行下一步操作。

代碼如下
String httpUrl=”http://XXX:8080/XXX/XXX/doLogin.ac”;
//HttpPost連接對象
HttpPost httpRequest=new HttpPost(httpUrl);
//使用NameValuePair來保存要傳遞的post參數
List params=new ArrayList();
//添加要傳遞的參數
params.add(new BasicNameValuePair(“messageId”, messageid));
params.add(new BasicNameValuePair(“tokenId”, tokenid));
//設置字符集
HttpEntity httpEntity=new UrlEncodedFormEntity(params,”utf-8”);
//請求httprequest
httpRequest.setEntity(httpEntity);

    //取得默認的HttpClient
    HttpClient httpClient=new DefaultHttpClient();
    //取得HttpResponse
    HttpResponse httpResponse=httpClient.execute(httpRequest);
    //HttpStatus.SC_OK表示連接成功
    if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
        //取得返回的字符串
        String strResult=EntityUtils.toString(httpResponse.getEntity());
        System.out.println(strResult);
    }else{
        int ok=httpResponse.getStatusLine().getStatusCode();
        System.out.println(ok);
        System.out.println("請求錯誤");
    }

但是通過debug發現,程序總是不執行HttpResponse httpResponse=httpClient.execute(httpRequest);這句代碼。因此,服務端也無法獲得請求。通過查找發現,httpClient被回收釋放了。

解決方法如下:

private HttpPost httpRequest;
//取得默認的HttpClient
private HttpClient httpClient=new DefaultHttpClient();
HttpResponse response;
public void httpPost(String messageid, String tokenid) throws Exception {

    /tp地址
    String httpUrl="http://XXX:8080/XXX/XXX/messageFeedBack.ac";
    //HttpPost連接對象
    try{
         httpRequest=new HttpPost(httpUrl);
        //使用NameValuePair來保存要傳遞的post參數
        List<NameValuePair> params=new ArrayList<NameValuePair>();
        //添加要傳遞的參數
        params.add(new BasicNameValuePair("messageId", messageid));
        params.add(new BasicNameValuePair("tokenId", tokenid));
        //設置字符集
        HttpEntity httpEntity=new UrlEncodedFormEntity(params,"utf-8");
        //請求httprequest
        httpRequest.setEntity(httpEntity);
        if(httpClient!=null){
            Thread thread=new Thread(){
                @Override
                public void run(){
                    try {
                         response=httpClient.execute(httpRequest);
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        response=null;
                        interrupted();
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        response=null;
                        interrupted();
                        e.printStackTrace();
                    }
                }
            };
            thread.start();
            try{
                Thread.sleep(1000*2);
                if(response==null){
                    thread.interrupt();
                }
            }catch(InterruptedException e){
                e.printStackTrace();
            }

        }
        int statusCode=response.getStatusLine().getStatusCode();
        System.out.println(statusCode);
        //HttpStatus.SC_OK表示連接成功
        if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
            //取得返回的字符串
            String strResult=EntityUtils.toString(response.getEntity());
            System.out.println(strResult);
        }else{
            String strResult=EntityUtils.toString(response.getEntity());
            System.out.println(strResult);
            System.out.println("請求錯誤");
        }
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        httpClient.getConnectionManager().closeExpiredConnections();
    }

}
發佈了37 篇原創文章 · 獲贊 19 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章