1. 程式人生 > >android 獲取圖片驗證碼

android 獲取圖片驗證碼

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) {
        }

        @Override
public 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() { @Override
public void run() { // Image_yanzhengma.setImageBitmap(bitmap); } }); } }); }