zoukankan      html  css  js  c++  java
  • android发送HTTP请求模板 java程序员

    以get方式发送HTTP请求:

    private Thread getThread; 
    getThread = new Thread() 
    { 
        public void run() 
        { 
            HttpClient client = new DefaultHttpClient(); 
            HttpGet getMethod = new HttpGet(REQUEST_URL); 
            HttpResonse response = null; 
            try 
            { 
                response = client.execute(getMethod); 
                if(response != null) 
                { 
                    StatusLine statusLine = response.getStatusLine(); 
                    if(statusLine != null) 
                    { 
                        if(statusLine.getStatusCode() == HttpStatus.SC_OK) 
                        { 
                            HttpEntity entity = response.getEntity(); 
                            if(entity != null) 
                            { 
                                InputStream content = entity.getContent(); 
                                handleEntity(content); 
                            } 
                        } 
                    } 
                } 
            } 
            catch(ClientProtocolException e) 
            { 
                e.printStackTrace(); 
            } 
            catch(IOException e) 
            { 
            } 
            finally 
            { 
                client.getConnectionManager().shutdown(); 
            } 
        } 
        private void handleEntity(InputStream content) throws IOException 
        { 
            byte[] buffer = new byte[1024]; 
            String result = ""; 
            int length = -1; 
            StringBuilder sb = new StringBuilder(); 
            whlle(length = content.read(buffer) != -1) 
            { 
                String tempStr = new String(buffer, 0, length, Charset.forName(DEFAULT_ENCODING)); 
                sb.append(tempStr); 
            } 
            result = sb.toString(); 
            result = result.equals("") ? "nothing" : result; 
            content.close(); 
        } 
    } 
    getThread.start();
    


    以POST方式发送HTTP请求:

    private Thread postThread; 
    postThread = new Thread() 
    { 
        public void run() 
        { 
            HttpParams params = new BasicHttpParams(); 
            HttpConnectionParams.setConnectionTimeout(params, 5000); 
            HttpPost postMethod = new HttpPost(REQUEST_URL); 
            HttpClient client = new DefaultHttpClient(params); 
            HttpResponse response = null; 
            List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
            nvps.add(new BasicNameValuePair("email", str1)); 
            nvps.add(new BasicNameValuePair("password", str2)); 
            try 
            { 
                postMethod.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 
                response = client.execute(postMethod); 
                handleResponse(response); 
            } 
            catch(Exception e) 
            { 
                e.printStackTrace(); 
            } 
            finally 
            { 
                client.getConnectionManager().shutdown(); 
            } 
        } 
        private void handleResponse(HttpResponse response) 
        { 
            if(response != null) 
            { 
                StatusLine statusLine = response.getStatusLine(); 
                if(statusLine != null) 
                { 
                    int responseCode = statusLine.getStatusCode(); 
                    if(responseCode == HttpStatus.SC_OK) 
                    { 
                        //TO DO LIST 
                    } 
                } 
            } 
        } 
    }; 
    postThread.start();


     

  • 相关阅读:
    JavaScript高级第01天笔记
    JavaScript基础第06天笔记
    JavaScript基础第05天笔记
    JavaScript基础第04天笔记
    JavaScript基础第03天笔记
    JavaScript基础第02天笔记
    JavaScript基础第01天笔记
    03-CSS文字文本样式
    CSS第二天笔记
    浮动
  • 原文地址:https://www.cnblogs.com/java20130725/p/3215853.html
Copyright © 2011-2022 走看看