android发送HTTP请求模板

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


 

posted @ 2013-02-09 15:16  java程序员-c  阅读(298)  评论(0编辑  收藏  举报