关闭

http连接 用apache 进行basic认证

165人阅读 评论(0) 收藏 举报
分类:

public class TestHttpsActivity extends Activity {
 Button button;
 ImageView image;
 Bitmap bitmap = null;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  button = (Button) findViewById(R.id.sendImage);
  image = (ImageView) findViewById(R.id.image);
  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    //connect();
    new Thread() {
     public void run() {
      while(true){
       try {
        Thread.sleep(200);
       } catch (InterruptedException e) {
        e.printStackTrace();
       }
       connect();
      }
     };

    }.start();
   }
  });
 }

 Handler handle = new Handler() {
  public void handleMessage(Message msg) {
   if (msg.what == 1) {
    if(bitmap!=null)
    image.setImageBitmap(bitmap);
    Log.i("bitmap", bitmap + "");
   }

  };
 };

 private void connect() {
  String urlStr = "http://192.168.0.128/snapshot.cgi";
  HttpGet request = new HttpGet(urlStr);
  request.setHeader("Authorization", "Basic "
    + Base64.encodeToString("admin:hbrc123".getBytes(),
      Base64.NO_WRAP));
  HttpClient httpclient = new DefaultHttpClient();
  try {
   HttpResponse httpResponse = httpclient.execute(request);
   if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    Log.i("true", "true");
    HttpEntity httpEntity = httpResponse.getEntity();
    InputStream inputStream = httpEntity.getContent();
    bitmap = BitmapFactory.decodeStream(inputStream);
    Log.i("bitmap", bitmap + "");
    inputStream.close();
    Message message = new Message();
    message.what = 1;
    handle.sendMessage(message);

   }
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

 }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22857次
    • 积分:189
    • 等级:
    • 排名:千里之外
    • 原创:95篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档