1. 程式人生 > >get網路展示listview列表

get網路展示listview列表

package com.bw.ymy.text3;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private ListView listview;
private final int UPDATE_UI=1;
private MyBase adapter;
private Handler handler=new Handler()
{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what)
{
case UPDATE_UI:

            List<User.ResultBean.DataBean> datas=(List<User.ResultBean.DataBean>) msg.obj;
            adapter.setdata(datas);
            break;

    }
}

};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=findViewById(R.id.listview);
if(!Nettill.newwork(MainActivity.this))
{
Toast.makeText(MainActivity.this,“無網路”,Toast.LENGTH_LONG).show();
}
adapter=new MyBase(this);
listview.setAdapter(adapter);

    new Thread(
            new Runnable() {
                @Override
                public void run() {
                    getdata();
                }
            }
    ).start();
}
private String Base="http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri=tt";
private void getdata()
{
    try {
        URL url=new URL(Base);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setConnectTimeout(5000);
        urlConnection.setReadTimeout(5000);
        int responseCode = urlConnection.getResponseCode();
        if(responseCode==200)
        {
            String result=stream2(urlConnection.getInputStream());
           User user=new Gson().fromJson(result,User.class);
           handler.sendMessage(handler.obtainMessage(UPDATE_UI,user.getResult().getData()));


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

}
private String stream2(InputStream is)throws IOException
{
    InputStreamReader InputStreamReader=new InputStreamReader(is);
    BufferedReader BufferedReader=new BufferedReader(InputStreamReader);
    StringBuilder StringBuilder=new StringBuilder();
    for (String tmp=BufferedReader.readLine();tmp!=null;tmp=BufferedReader.readLine())
    {
        StringBuilder.append(tmp);
    }
    return  StringBuilder.toString();
}

}

Adapter
:
package com.bw.ymy.text3;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyBase extends BaseAdapter {
private List<User.ResultBean.DataBean> list;
private Context context;

public MyBase( Context context) {
    list = new ArrayList<>();
    this.context = context;
}
//更新

public void setdata(List<User.ResultBean.DataBean> list)
{
this.list=list;
notifyDataSetChanged();

}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if(convertView==null)
    {
        convertView=LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        holder=new ViewHolder();
        holder.text1=convertView.findViewById(R.id.text1);
        convertView.setTag(holder);
    }else
    {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.text1.setText(list.get(position).getTitle());
    return convertView;
}
class ViewHolder{
    TextView text1;
}

}

判斷網路
package com.bw.ymy.text3;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Nettill {
//判斷網路
public static boolean newwork(Context context)
{
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
return activeNetworkInfo!=null && activeNetworkInfo.isAvailable();

}
//判斷手機網路
public static  boolean isWork(Context context)
{
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
    return  activeNetworkInfo.getType()==manager.TYPE_MOBILE;
}

}