package com.example.lxss; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import com.google.gson.Gson; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends AppCompatActivity { private Button button; private Button button1; private Bean bean; private ListView lv; private Mydapter ma; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button1 = (Button) findViewById(R.id.button2); lv = (ListView) findViewById(R.id.lv); ma=new Mydapter(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new AsyncTask<String,String,String>() { @Override protected String doInBackground(String... strings) { return getData(); } @Override protected void onPostExecute(String str) { Gson gson = new Gson(); bean = gson.fromJson(str, Bean.class); lv.setAdapter(ma); super.onPostExecute(str); }; }.execute(); } }); } class Mydapter extends BaseAdapter{ @Override public int getCount() { return bean.getNewslist().size(); } @Override public Object getItem(int i) { return bean.getNewslist().get(i ); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHoder vi; if (view==null){ vi = new ViewHoder(); view = View.inflate(MainActivity.this, R.layout.list, null); vi.textView3=(TextView) view.findViewById(R.id.textView3); vi.textView4=(TextView) view.findViewById(R.id.textView4); vi.textView5=(TextView) view.findViewById(R.id.textView5); vi.textView6=(TextView) view.findViewById(R.id.textView6); vi.tvs=(TextView) view.findViewById(R.id.tvs); view.setTag(vi); }else { vi = (ViewHoder) view.getTag(); } vi.textView3.setText(bean.getNewslist().get(i).getCtime()); vi.textView4.setText(bean.getNewslist().get(i).getDescription()); vi.textView5.setText(bean.getNewslist().get(i).getPicUrl()); vi.textView6.setText(bean.getNewslist().get(i).getTitle()); vi.tvs.setText(bean.getNewslist().get(i).getUrl()); return view; } class ViewHoder{ public TextView tvs; public TextView textView3; public TextView textView4; public TextView textView5; public TextView textView6; } } private String getData(){ String str=""; try { URL url = new URL("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); int code = conn.getResponseCode(); if (code==200) { InputStream stream = conn.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len=-1; byte[] b = new byte[1024]; while ((len=stream.read(b))!=-1){ bos.write(b,0,len); } str = new String (bos.toByteArray(),"GBK"); } } catch (Exception e) { e.printStackTrace(); } return str; } }
异步请求网络json
最新推荐文章于 2018-04-15 20:23:09 发布