<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="www.hui2wulian.phpapplication2.MainActivity"> <Button android:id="@+id/btn_getstring" android:layout_width="wrap_content" l android:layout_height="wrap_content" android:text="普通字符串"/> <Button android:id="@+id/btn_getjsonstring" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="json字符串"/> <ListView android:id="@+id/lv_listview" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <WebView xmlns:android="" xmlns:app="" xmlns:tools="" l android:layout_width="match_parent" android:layout_height="match_parent" tools:context="www.hui2wulian.phpapplication2.MainActivity"><WebView android:id="@+id/wv" android:layout_width="match_parent" android:layout_height="match_parent"> </WebView> </WebView>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv1" l android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv1" MainActivity.java android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
package www.hui2wulian.phpapplication2; t.SharedPreferences; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; le.gson.Gson; flect.TypeToken; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.HttpURLConnection; import java.URL; import java.util.List; public class PhpActivity extends AppCompatActivity {List<Stu> stulist=null; private Handler handler=new Handler(){ PhpActivity.java@Override public void handleMessage(Message msg) {super.handleMessage(msg); String abc=(String) msg.obj; Toast.makeText(PhpActivity.this,"返回的字符串为:"+abc,Toast.LENGTH_SHORT).show(); switch (msg.what){case 200:break; case -1:break; case -2:break; default:}}}; @Override protected void onCreate(Bundle savedInstanceState) {Create(savedInstanceState); setContentView(R.layout.activity_php); final ListView listView=(ListView)findViewById(R.id.lv_listview); Button btn_getstring=(Button)findViewById(R.id.btn_getstring); Button btn_getjsonstring=(Button)findViewById(R.id.btn_getjsonstring); btn_getstring.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {if (getDataFromCache()){StuAdapter stuAdapter=new StuAdapter(stulist,PhpActivity.this); listView.setAdapter(stuAdapter); }else {getDateFromService(); }final String path="10.10.192.69/untitled2/index4.php"; new Thread(){@Override public void run() {super.run(); try {URL url=new URL(path); HttpURLConnection conn=(HttpURLConnection)url.openConnection(); conn.setConnectTimeout(5000); int codeResponseCode(); if (code==200){InputStream inputStreamInputStream(); String abc=convertStreamToString(inputStream); Message message=new Message(); message.obj=abc; handler.sendMessage(message); }else{}} catch (Exception e) {e.printStackTrace(); }}}.start(); Toast.makeText(PhpActivity.this,"完成",Toast.LENGTH_SHORT).show(); }}); btn_getjsonstring.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {Toast.makeText(PhpActivity.this,"完成",Toast.LENGTH_SHORT).show(); }}); }public static String convertStreamToString(InputStream is) {BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line =null ; try {while ((line = adLine()) != null) {sb.append(line + "n"); }} catch (IOException e) {e.printStackTrace(); } finally {try {is.close(); } catch (IOException e) {e.printStackTrace(); }}String(); }private void getDateFromService() {final String path = "10.10.192.1/stumanage/2.php"; // 开线程 new Thread() {@Override public void run() {super.run(); //获取数据 try {URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); //必须大写 conn.setConnectTimeout(5000); Log.d("kk", "try -1 "); int code = ResponseCode(); if (code == 200) {Log.d("zh", "code==200 "); //请求成功 InputStream inputStream = InputStream(); //然后把输入流转为字符串 。上网找一个把流转为字符串的函数 String abc = convertStreamToString(inputStream); //解析json字符串 Gson gson = new Gson(); List<Stu> stulist = gson.fromJson(abc, new TypeToken<List<Stu>>() {}.getType()); // 程序如果走到这个位置 。说明数据符合json格式。这时候我要把数据使用sharedpreference保存起来 //android 内存缓存 SharedPreferences sp = getSharedPreferences("person", MODE_PRIVATE); //person是文件名. SharedPreferences.Editor editor = sp.edit(); editor.putString("studata", abc);//按照关键字 studata。 把字符串保存起来 Log.d("kk", "stulist 获取数据正常"); Message message = new Message(); message.obj = abc; //定义带有数据的消息 message.what = 202; //202 代码获取数据正常 handler.sendMessage(message); Log.d("kk", "发送消息正常"); } else {//请求失败 Message message = new Message(); message.what = -1; //-1 代码获取数据失败 handler.sendMessage(message); Log.d("kk", "请求失败"); }} catch (Exception ex) {Message message = new Message(); message.what = -2; //-1 代码获取数据时发生异常情况 //发送消息 handler.sendMessage(message); Log.d("kk", "Exception"); }}}.start(); }private boolean getDataFromCache(){try {SharedPreferences sp = SharedPreferences("person", MODE_PRIVATE); String data = sp.getString("data", ""); if (data != "") {Gson gson = new Gson(); List<Stu> stulist = gson.fromJson(data, new TypeToken<List<Stu>>() {}.getType()); //暂时没有时间过期的判断 return true; } else {stulist=null; return false; }}catch (Exception ex){ex.printStackTrace(); stulist=null; return false; }} }
package www.hui2wulian.phpapplication2; import android.app.Activity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; /** * Created by 可 on 2017/5/18. */ public class StuAdapter extends BaseAdapter { StuAdapter.javapublic List<Stu> stulist; public Activity activity; public StuAdapter(List<Stu> list, Activity activity){this.stulist=list; this.activity=activity; }@Override public int getCount() {return stulist.size(); }@Override public Object getItem(int position) {(position); }@Override public long getItemId(int position) {return position; }@Override public View getView(int position, View convertView, ViewGroup parent) {View viewLayoutInflater().inflate(R.layout.activity_php,null); TextView textView_name=(TextView)view.findViewById(R.id.tv1); TextView textView_age=(TextView)view.findViewById(R.id.tv2); Stu stu(position); textView_name.Name()); textView_age.Age()+""); return view; } }
package www.hui2wulian.phpapplication2; /** * Created by 可 on 2017/5/18. */ public class Stu {private int age; private String name; public Stu() {super(); Stu.java }public Stu(int age, String name) {super(); this.age = age; this.name = name; }public int getAge() {return age; }public void setAge(int age) {this.age = age; }public String getName() {return name; }public void setName(String name) {this.name = name; } }
本文发布于:2024-02-01 00:22:58,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170671818132465.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |