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();


     

  • 相关阅读:
    C语言基于单链表得学生成绩管理系统
    C语言实现扫雷小程序外挂,棒棒的
    小白学习C语言一定要掌握的那些知识点!
    C语言快速入门教程之10分钟快速掌握数据类型
    神奇的C语言,这才是C语言大牛操作,作为面试题,怕是秒杀众人
    多线程
    java基础- Collection和map
    String 和 new String
    idea快捷键
    用bootstrap 分页插件问题
  • 原文地址:https://www.cnblogs.com/java20130725/p/3215853.html
Copyright © 2011-2022 走看看