android获取网络数据

本文详细介绍了Android平台下通过HTTP请求获取网络数据的方法,包括字符串、图片、文件三种不同类型的数据获取流程及代码实现。通过实例演示了如何使用HttpClient进行网络请求,并解析响应内容为所需数据格式。

android获取网络数据有字符串、图片、文件对于不同的数据的获取方法如下
字符串:

public String getDataFromServer(String Urlpath) {
        String result="";
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(Urlpath);
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
            InputStream is = entity.getContent();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader bufferReader = new BufferedReader(isr);
            String inputLine = "";
            int i=1;
            while ((inputLine = bufferReader.readLine()) != null) {
                  result += inputLine + "\n";
                Log.i("getDataFromServer","inputLine"+i+":"+inputLine);
                i=i+1;
            }
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

图片:

public Bitmap getHttpBitmap(final String url) {
                try {

                    myFileURL = new URL(url);
                    HttpURLConnection conn = (HttpURLConnection) myFileURL
                            .openConnection();
                    conn.setConnectTimeout(6000);
                    conn.setDoInput(true);
                    conn.setUseCaches(false);
                    InputStream is = conn.getInputStream();
                    BitmapFactory.Options   opts = new BitmapFactory.Options();
                    opts.inJustDecodeBounds=true;
                    byte[] data = getBytes(is);
                    BitmapFactory.decodeByteArray(data, 0,data.length,opts);
                    Log.i("ImageLoader","width="+opts.outWidth+",height="+opts.outHeight);
                    opts.inJustDecodeBounds=false;
                    opts.inSampleSize=calculateInSampleSize(opts,150,300);
                    opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
                    bitmap = BitmapFactory.decodeByteArray(data, 0,data.length,opts);
                    Log.i("ImageLoader","houwidth="+bitmap.getWidth()+",houheight="+bitmap.getHeight());
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("HttpUtils","getHttpBitmap异常");
                }finally{
                    if(is!=null){
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
        return bitmap;

    }

文件:

public String DownloadFile(String fileURL, File directory) {

        String code = "";
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(fileURL);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                code = String.valueOf(response.getStatusLine().getStatusCode());
                FileOutputStream f = new FileOutputStream(directory);
                int l;
                byte[] tmp = new byte[2048];

                while ((l = instream.read(tmp)) != -1) {
                    f.write(tmp, 0, l);
                }

                f.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
            Log.i("HttpUtils","DownloadFile异常");
        } finally {

        }

        return code;

    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值