一, package com.example.zhoukaolianxi1; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView first; private TextView second; private HUCFragment hucFragment; private HClientFragment hClientFragment; private FragmentManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); first = findViewById(R.id.first); second = findViewById(R.id.second); hucFragment = new HUCFragment(); hClientFragment = new HClientFragment(); manager = getSupportFragmentManager(); manager.beginTransaction() .add(R.id.content,hucFragment) .add(R.id.content,hClientFragment) .hide(hClientFragment) .commit(); first.setOnClickListener(this); second.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.first: manager.beginTransaction().show(hucFragment).hide(hClientFragment).commit(); break; case R.id.second: manager.beginTransaction().hide(hucFragment).show(hClientFragment).commit(); break; } } }
二,
package com.example.zhoukaolianxi1; import android.net.http.AndroidHttpClient; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; /** * Created by 。。。。 on 2018/9/1. */ public class HttpUtils { public static String Doget(String urlstring){ String result=""; try { URL url=new URL(urlstring); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoInput(true); connection.setDoOutput(false); connection.setUseCaches(false); connection.setConnectTimeout(3000); connection.connect(); int code=connection.getResponseCode(); if(code==200){ InputStream is=connection.getInputStream(); result =jeixi(is); } } catch (Exception e) { e.printStackTrace(); } return result; } public static String get(String urlstring){ String result=""; HttpClient client=new DefaultHttpClient(); HttpGet httpGet=new HttpGet(urlstring); try { HttpResponse response=client.execute(httpGet); int code=response.getStatusLine().getStatusCode(); if(code==200){ InputStream is=response.getEntity().getContent(); result=jeixi(is); } } catch (IOException e) { e.printStackTrace(); } return result; } private static String jeixi(InputStream is) { String result=""; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b=new byte[1024]; int coutent=-1; while((coutent=is.read(b,0,b.length))!=-1){ baos.write(b,0,coutent); baos.flush(); } result=baos.toString(); baos.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } return result; } }
三,
package com.example.zhoukaolianxi1; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import com.google.gson.Gson; import java.net.URL; import java.util.ArrayList; import java.util.List; /** * Created by 。。。。 on 2018/9/1. */ public class HClientFragment extends Fragment { private static final String URL = "http://120.27.23.105/product/getProductCatagory?cid=1"; private ListView listView; private List<Categroy.DataBean> list; private CategroyAdapter adapter; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v=inflater.inflate(R.layout.first_fragment,container,false); listView = v.findViewById(R.id.lv_data); return v; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); list = new ArrayList<>(); adapter = new CategroyAdapter(getActivity(),list); listView.setAdapter(adapter); new AsyncTask<String,Integer,String>(){ @Override protected String doInBackground(String... strings) { String result=HttpUtils.get(strings[0]); return result; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if(!TextUtils.isEmpty(s)){ Gson gson=new Gson(); Categroy categroy=gson.fromJson(s,Categroy.class); list.clear(); list.addAll(categroy.getData()); adapter.notifyDataSetChanged(); } } }.execute(URL); } }
四,
package com.example.zhoukaolianxi1; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import com.google.gson.Gson; import java.net.URL; import java.util.ArrayList; import java.util.List; /** * Created by 。。。。 on 2018/9/1. */ public class HUCFragment extends Fragment { private static final String URL = "https://www.zhaoapi.cn/quarter/getJokes?source=android&appVersion=101"; private ListView listView; private JokeAdapter adapter; private List<Joke.DataBean> list; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 123: String result= (String) msg.obj; if(!TextUtils.isEmpty(result)){ Gson gson=new Gson(); Joke joke=gson.fromJson(result,Joke.class); Log.i("aaaa",joke+""); list.clear(); list.addAll(joke.getData()); adapter.notifyDataSetChanged(); } break; } } }; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v=inflater.inflate(R.layout.first_fragment,container,false); listView = v.findViewById(R.id.lv_data); return v; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); list = new ArrayList<>(); adapter = new JokeAdapter(getActivity(), list); listView.setAdapter(adapter); new Thread(new Runnable() { @Override public void run() { String result=HttpUtils.Doget(URL); Message msg=Message.obtain(); msg.what=123; msg.obj=result; handler.sendMessage(msg); } }).start(); } }