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


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值