直接读取文件流的方式

直接读取文件流的方式

 HttpClient httpClient = new DefaultHttpClient();
        String reqStr = new JSONUtil().ObjectToJsonStr(commonRequestBean);
        String paramImg = URLEncoder.encode(ContentAesQos.getParamImg(reqStr));
        String url = CommonQos.NOSATISFACTION_IMG_QUERY+"?reqParam="+paramImg;
        System.out.println("url:"+url);
        //第二步:生成使用POST方法的请求对象
        HttpGet httpPost = new HttpGet(url);
        httpPost.addHeader("Content-Type", "application/json");
//        String reqStr = new JSONUtil().ObjectToJsonStr(commonRequestBean);
//        String paramImg = ContentAesQos.getParamImg(reqStr);
//        httpPost.setEntity(new StringEntity(paramImg));
        //执行请求对象
        try {
            //第三步:执行请求对象,获取服务器发还的相应对象
            HttpResponse response = httpClient.execute(httpPost);
            //第四步:检查相应的状态是否正常:检查状态码的值是200表示正常
            Log.e("HTTP", response.getStatusLine().getStatusCode() + "");
            if (response.getStatusLine().getStatusCode() == 200) {
                //第五步:从相应对象当中取出数据,放到entity当中
                filePath = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/" + "qos_temp.png"; // 文件路径
                File file = new File(filePath);
                FileOutputStream outputStream = new FileOutputStream(file);
                InputStream inputStream = response.getEntity()
                        .getContent();
                byte b[] = new byte[1024];
                int j = 0;
                while ((j = inputStream.read(b)) != -1) {
                    outputStream.write(b, 0, j);
                }
                outputStream.flush();
                outputStream.close();

//                setImgView(img);
                handler.sendEmptyMessage(2);

get post 都可以

你可能感兴趣的:(直接读取文件流的方式)