作者:bell723_893 | 来源:互联网 | 2023-07-25 16:41
android:layout_
android:layout_>
android:id="@+id/web_view"
android:layout_
android:layout_>
package com.example.a02_httpurlconnnection_newstop_03;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_layout);
WebView webView = (WebView) findViewById(R.id.web_view);
Intent intent = getIntent();
String url = intent.getStringExtra("url");
//加载
webView.loadUrl(url);
//设置
webView.setWebViewClient(new WebViewClient());
WebSettings settings = webView.getSettings();
settings.setJavascriptEnabled(true);
settings.setJavascriptCanOpenWindowsAutomatically(true);
}
}
package com.example.a02_httpurlconnnection_newstop_03;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.a02_httpurlconnnection_newstop_03.adapter.MyAdapter;
import com.example.a02_httpurlconnnection_newstop_03.bean.DataBean;
import com.example.a02_httpurlconnnection_newstop_03.bean.DataResultBean;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
//接收数据...设置适配器
if (msg.what == 0){
DataBean dataBean = (DataBean) msg.obj;
list = dataBean.getResult().getData();
//设置适配器
MyAdapter myAdapter = new MyAdapter(list, MainActivity.this);
listView.setAdapter(myAdapter);
}
}
};
private List list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
//条目的点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
//传值过去...url路径
if (list != null){
Intent intent = new Intent(MainActivity.this,WebActivity.class);
intent.putExtra("url",list.get(i).getUrl());
startActivity(intent);
}
}
});
}
public void getTop(View view){
//点击获取网络数据之前,判断一下网络是否可用...不可用,设置
if (isNetConnected()){
getNetData();
}else {
//对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("警告");
builder.setMessage("网络不可用,是否设置?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Settings.ACTION_WIRELESS_SETTINGS
//Settings.ACTION_WIFI_SETTINGS
Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(intent);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
}
}
/**
* 判断网络是否可用
*
* boolean
*/
private boolean isNetConnected(){
//1.获取一个连接管理对象
ConnectivityManager manager = (ConnectivityManager) MainActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
//2.获取网络连接的信息
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo != null){
//返回值就代表网络是否可用
return networkInfo.isAvailable();
}
return false;
}
/**
* 获取头条的数据
*/
private void getNetData() {
new Thread(){
@Override
public void run() {
//头条服务器地址
String path = "http://v.juhe.cn/toutiao/index";
try {
URL url = new URL(path);
//打开连接
HttpURLConnection cOnnection= (HttpURLConnection) url.openConnection();
//设置
connection.setRequestMethod("POST");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
//post方式要提交参数给服务器...以流的方式提交给服务器
connection.setDoOutput(true);
//设置请求参数的类型
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
String params = "type=top&key=597b4f9dcb50e051fd725a9ec54d6653";
//参数以流的形式写给服务器...字节数组的形式写出去
connection.getOutputStream().write(params.getBytes());
//响应
int respOnseCode= connection.getResponseCode();
if (respOnseCode== 200){
//获取输入字节流
InputStream inputStream = connection.getInputStream();
String json = streamToString(inputStream,"utf-8");
Log.i("json",json);
//解析...解析完成之后发送到主线程,设置适配器
Gson gson = new Gson();
DataBean dataBean = gson.fromJson(json, DataBean.class);
//发送出去
Message message = Message.obtain();
message.what = 0;
message.obj = dataBean;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
private String streamToString(InputStream inputStream,String charset) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String s = null;
StringBuilder builder = new StringBuilder();
while ((s = bufferedReader.readLine()) != null){
builder.append(s);
}
bufferedReader.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}