package com.example.httptest;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;import android.widget.TextView;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import org.json.JSONException;import org.json.JSONObject;import java.io.IOException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class MyActivity extends Activity {    TextView textView;    Button button;String location;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        final Handler handler = new Handler(){            @Override            public void handleMessage(Message msg) {                super.handleMessage(msg);                switch (msg.what){                    case 1:                        textView.setText(location);                }            }        };        setContentView(R.layout.main);        textView = (TextView) findViewById(R.id.text);        button = (Button) findViewById(R.id.fetch_button);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                new Thread(new Runnable() {                    @Override                    public void run() {                        String urlString = "https://db.tt/vYI4UVeb";                        URL url = null;                        try {                            url = new URL(urlString);                            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();                            urlConnection.setInstanceFollowRedirects(false);                            location = urlConnection.getHeaderField("location");                            handler.sendEmptyMessage(1);                        } catch (MalformedURLException e) {                            e.printStackTrace();                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                }).start();            }        });    }}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android Studio 中module需要单独引用jdk
  2. Android中的onTouch()事件如何区分单击事件
  3. 判断scrollview中某子控件是否可见
  4. android studio gradle依赖库使用国内资
  5. Android集成React Native实现多Tab页
  6. Android标题头滑动渐变,Titlebar滑动渐变,
  7. ANDROID 8.1 多个apk同时录音
  8. Android解析自定义xml文件(方案一)
  9. failed to set system property
  10. Android 如何获取RadioGroup选中RadioBut