Android 通过短链接,获取长连接
16lz
2021-01-23
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(); } }); }}
更多相关文章
- Android 自定义ProgressBar--进度自己设置图片
- Android 本地图片多选
- Android圆角图片封装类
- Android UI控件详解-ImageSwitcher(图片切换器)
- Android 之获取图片及视频缩略图
- Android为图片添加水印文字
- Android:缩小图片尺寸,并添加水印
- Android保存图片到图库,Android扫描文件到媒体库,Android保存图片
- Android 框架学习4:一次读懂热门图片框架 Picasso 源码及流程