马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
修改上次的项目,在activity_main.xml新添加个按钮<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="change_text"
android:id="@+id/change_text" />
<Button
android:id="@+id/asy"
android:text="use asyncText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
在gradle里dependencies标签下添加依赖implementation 'com.squareup.okhttp3:okhttp:3.4.1'
在AndroidManifest.xml里manifest标签下添加权限<uses-permission android:name="android.permission.INTERNET"/>
修改mainactivity.javapackage com.example.xinwei.androidthreadtest;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public static final int UPDATE_TEXT=1;
private static TextView text;
@SuppressLint("HandlerLeak")
private static Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case UPDATE_TEXT:
text.setText("Nice to meet you");
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(TextView)findViewById(R.id.text);
Button changeText=(Button)findViewById(R.id.change_text);
Button asyncTask=(Button)findViewById(R.id.asy);
asyncTask.setOnClickListener(this);
changeText.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.change_text:
new Thread(){
@Override
public void run() {
Message message=new Message();
message.what=UPDATE_TEXT;
handler.sendMessage(message);
}
}.start();
break;
case R.id.asy:
MyAsyncTask myAsyncTask=new MyAsyncTask();
myAsyncTask.execute("http://www.fishC.com");
default:
break;
}
}
private class MyAsyncTask extends AsyncTask<String,Void,String>{
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
text.setText(s);
}
@Override
protected String doInBackground(String... strings) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(strings[0])
.build();
Response response = null;
try {
response = client.newCall(request).execute();
String responseData = response.body().string();
return responseData;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
这是经常用的获取网路资源的方法,简单又好用,直接就可以在onPostExecute方法里修改UI,因为升级了新版本我们的模拟器不用genymotion了,用谷歌自己的模拟器速度也很快了,效果图:
|