android 獲取圖片驗證碼
阿新 • • 發佈:2018-11-12
public void getURLimage(String url) { //獲取圖片驗證碼的 方法 OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Overridepublic void onResponse(Call call, Response response) throws IOException { Headers headers = response.headers(); Set<String> names = headers.names(); for (String name:names){ // if("Set-Cookie".equals(name)){ // JSESSIONID=headers.get(name); // } } InputStream is = response.body().byteStream(); //獲取 位元組輸入流 final Bitmap bitmap = BitmapFactory.decodeStream(is); // 把獲取到的 資料 轉換成 Bitmap 型別的 getActivity().runOnUiThread(new Runnable() { @Overridepublic void run() { // Image_yanzhengma.setImageBitmap(bitmap); } }); } }); }