俗话说,好记性不如烂笔头。今天终于体会其中的道理了。昨天写好的代码不知道为何找不到了。所以今天我一定得抽出一点时间把我的代码保存起来,以防我的代码再没有了。

还是先上图片。

这个界面是用ListView实现的,数据是数据库里面的数据,服务器为thinkPHP。我就不废话那么多,直接把我的代码拷贝上了、

总的思想就是,利用账号查询数据库中的信息,然后对返回的信息进行解析。这里我把账号保存到了本地。

Data.java

package cn.edu.aynu.rjxy.entity;

public class Data {
private int id;
private String exp_name;
private String exp_tech;
private String exp_type;
private String exp_source;
private String exp_tno;
private String istate;
private String sno;
private String sname;
private String passwd;
private String grade;
private String school;
private String qq;
private String clas;
private String cellphone;
private String email;
private String spec;
public String getSpec() {
return spec;
}
public void setSpec(String spec) {
this.spec = spec;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getClas() {
return clas;
}
public void setClas(String clas) {
this.clas = clas;
}
public String getCellphone() {
return cellphone;
}
public void setCellphone(String cellphone) {
this.cellphone = cellphone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
} public String getExp_type() {
return exp_type;
}
public void setExp_type(String exp_type) {
this.exp_type = exp_type;
}
public String getExp_source() {
return exp_source;
}
public void setExp_source(String exp_source) {
this.exp_source = exp_source;
}
public String getExp_tno() {
return exp_tno;
}
public void setExp_tno(String exp_tno) {
this.exp_tno = exp_tno;
}
public String getIstate() {
return istate;
}
public void setIstate(String istate) {
this.istate = istate;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getExp_name() {
return exp_name;
}
public void setExp_name(String exp_name) {
this.exp_name = exp_name;
}
public String getExp_tech() {
return exp_tech;
}
public void setExp_tech(String exp_tech) {
this.exp_tech = exp_tech;
}
@Override
public String toString() {
return "Data [id=" + id + ", exp_name=" + exp_name + ", exp_tech="
+ exp_tech + "]";
} }
SharedPreferencesUtils.java
package cn.edu.aynu.rjxy.utils;

import java.util.HashMap;
import java.util.Map; import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; public class SharedPreferencesUtils {
//保存账号和密码到minemessage.xml
public static boolean saveUserInfo(Context context,String cellphone,String qq,String email){
SharedPreferences sp = context.getSharedPreferences("minemessage", Context.MODE_PRIVATE);
Editor edit = sp.edit();
edit.putString("cellphone", cellphone);
edit.putString("qq", qq);
edit.putString("email", email);
edit.commit();
return true;
}
//保存账号和密码到data.xml
public static boolean saveUserInfo02(Context context,String sno,String password,String spinnerId){
SharedPreferences sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);
Editor edit = sp.edit();
edit.putString("sno", sno);
edit.putString("password", password);
edit.putString("spinnerId", spinnerId);
edit.commit();
return true;
}
//保存账号和密码到select.xml
public static boolean saveUserInfo03(Context context,String sno,String id){
SharedPreferences sp = context.getSharedPreferences("select", Context.MODE_PRIVATE);
Editor edit = sp.edit();
edit.putString("sno", sno);
edit.putString("id", id);
edit.commit();
return true;
} //从data.xml文件中获取存贮的账号和密码
public static Map<String,String> getUserInfo(Context context){
SharedPreferences sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);
String sno = sp.getString("sno", null);
String password = sp.getString("password", null);
String spinnerId = sp.getString("spinnerId", null);
Map<String,String> userMap = new HashMap<String, String>();
userMap.put("sno", sno);
userMap.put("password", password);
userMap.put("spinnerId", spinnerId);
return userMap;
}
//从minemessage.xml文件中获取存贮的账号和密码
public static Map<String,String> getUserInfo02(Context context){
SharedPreferences sp = context.getSharedPreferences("minemessage", Context.MODE_PRIVATE);
String cellphone = sp.getString("cellphone", null);
String qq = sp.getString("qq", null);
String email = sp.getString("email", null);
Map<String,String> userMap = new HashMap<String, String>();
userMap.put("cellphone", cellphone);
userMap.put("qq", qq);
userMap.put("email", email);
return userMap;
}
//从select.xml文件中获取存贮的账号和密码
public static Map<String,String> getUserInfo03(Context context){
SharedPreferences sp = context.getSharedPreferences("select", Context.MODE_PRIVATE);
String sno = sp.getString("sno", null);
String id = sp.getString("id", null);
Map<String,String> userMap = new HashMap<String, String>();
userMap.put("sno", sno);
return userMap;
}
}
StreamTools.java
package cn.edu.aynu.rjxy.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; public class StreamTools { public static String readStream(InputStream is){
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer))!=-1) {
baos.write(buffer,0,len);
}
baos.close();
return new String(baos.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
}
}

MineActivity.java
package cn.edu.aynu.rjxy.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import cn.edu.aynu.rjxy.entity.Data;
import cn.edu.aynu.rjxy.utils.SharedPreferencesUtils; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView; public class MineActivity extends Activity {
private static final int CHANGE_UI = 1;
private static final int ERROR = 2;
private ListView lv;
private List<Data> datas = new ArrayList<Data>();
//主线程创建消息处理器
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
if (msg.what == CHANGE_UI) {
try {
JSONArray arr = new JSONArray((String)msg.obj);
for (int i = 0; i < arr.length(); i++) {
JSONObject temp = (JSONObject) arr.get(i);
// Log.d("json", temp.getInt("id")+temp.getString("exp_name")+temp.getString("exp_tech"));
Data data = new Data();
data.setId(temp.getInt("id"));
data.setExp_name(temp.getString("exp_name"));
data.setExp_tech(temp.getString("exp_tech"));
data.setExp_source(temp.getString("exp_source"));
data.setExp_type(temp.getString("exp_type"));
data.setExp_tno(temp.getString("tname"));
data.setIstate(temp.getString("istate"));
//这个地方可以获取到值但是适配器那位0
datas.add(data); }
lv.setAdapter(new MyAdapter());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
};
protected String sno; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine);
lv = (ListView) findViewById(R.id.lv);
select();
//取出账号和密码
Map<String,String> userInfo = SharedPreferencesUtils.getUserInfo(this); if (userInfo != null) {
sno = userInfo.get("sno");
} } private void select(){
//子线程更新UI
new Thread(){
public void run(){
try {
StringBuilder builder = new StringBuilder();
String path = "http://10.6.78.254:2016/xampp/graduate/index.php/home/Student/test_check";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//区别2、请求方式post
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "Mozilla/5.0(compatible;MSIE 9.0;Windows NT 6.1;Trident/5.0)");
//区别3、必须指定两个请求的参数
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//请求的类型 表单数据
//System.out.println(sno);
String data = "sno="+sno;
conn.setRequestProperty("Content-Length", data.length()+"");//数据的长度
//区别4、记得设置把数据写给服务器
conn.setDoOutput(true);//设置向服务器写数据
byte[] bytes = data.getBytes();
conn.getOutputStream().write(bytes);//把数据以流的方式写给服务器
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"UTF-8"));
for(String s=reader.readLine();s!=null;s=reader.readLine())
{
builder.append(s);
}
String content = builder.toString();
//通知主线程更新UI
Message message = new Message();
message.what = CHANGE_UI;
message.obj = content;
handler.sendMessage(message);
}else{
Log.e(HomeActivity.class.toString(), "Failed");
}
} catch (ClientProtocolException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
} };
}.start();
}
class MyAdapter extends BaseAdapter{ @Override
public int getCount() {
Log.d("AAA", ""+datas.size());
return datas.size(); } @Override
public Object getItem(int position) { return datas.get(position);
} @Override
public long getItemId(int position) { return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(MineActivity.this, R.layout.ui_setting_select, null); TextView exp_name = (TextView) view.findViewById(R.id.tv_name);
TextView exp_tech = (TextView) view.findViewById(R.id.tv_tech);
TextView exp_type = (TextView) view.findViewById(R.id.tv_type);
TextView exp_source = (TextView) view.findViewById(R.id.tv_source);
TextView exp_tno = (TextView) view.findViewById(R.id.tv_tno); Data data = datas.get(position);
Log.d("aaaaa",datas.get(position).getExp_name() ); exp_name.setText(datas.get(position).getExp_name());
//Log.i("exp_name", datas.get(position).getExp_name());
exp_tech.setText(datas.get(position).getExp_tech());
exp_type.setText(datas.get(position).getExp_type());
exp_source.setText(datas.get(position).getExp_source());
exp_tno.setText(datas.get(position).getExp_tno()); return view;
} }
}
activity_mine.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:padding="5dp"
android:orientation="vertical" >
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp">
</ListView> </RelativeLayout>

item_mine.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:background="#FFFFFF"
android:orientation="vertical">
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginTop="5dip"/> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
style="@style/LTexTviewStyle"
android:text="毕业设计名称" /> <TextView
android:id="@+id/tv_name"
style="@style/TextViewStyle"
android:text="25" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.2dip"
android:layout_marginTop="5dip"
android:background="#bb000000"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
style="@style/LTexTviewStyle"
android:text="题目类型" /> <TextView
android:id="@+id/tv_type"
style="@style/TextViewStyle"
android:text="25" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.2dip"
android:layout_marginTop="5dip"
android:background="#bb000000"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
style="@style/LTexTviewStyle"
android:text="题目来源" /> <TextView
android:id="@+id/tv_source"
style="@style/TextViewStyle"
android:text="25" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.2dip"
android:layout_marginTop="5dip"
android:background="#bb000000"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
style="@style/LTexTviewStyle"
android:text="指导老师" /> <TextView
android:id="@+id/tv_tno"
style="@style/TextViewStyle"
android:text="25" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.2dip"
android:layout_marginTop="5dip"
android:background="#bb000000"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
style="@style/LTexTviewStyle"
android:text="技术要求" /> <TextView
android:id="@+id/tv_tech"
style="@style/TextViewStyle"
android:text="25" />
</RelativeLayout>
<View android:layout_width="fill_parent"
android:layout_height="0.2dip"
android:layout_marginTop="5dip"
android:background="#bb000000"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_reselect"
android:onClick="reselect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退选"
android:layout_alignParentRight="true"
android:textColor="#000000"
android:background="#FFFFFF"/> </RelativeLayout>
</LinearLayout>

PHP端的代码:

//Android端查看我的选题
public function test_check(){
$sno = I('sno');
$Experiment = M("Experiment");
//$sno = '130906008';
$exp_tno = $Experiment -> where("sno = '".$sno."'") -> getField('exp_tno');
//echo $exp_tno;
$RowCount = $Experiment -> where("sno = '".$sno."'") -> Count();
if($RowCount == 1){
$myModel = new \Think\Model();
$result=$myModel->query("select *from g_experiment,g_teacher where sno = $sno && tno = $exp_tno ;");
$this ->ajaxReturn($result);
}else{
echo '你还没有选题';
}
}

Android+PHP+MYSQL把数据库中的数据显示在Android界面上的更多相关文章

  1. 涂抹mysql笔记-数据库中的权限体系

    涂抹mysql笔记-数据库中的权限体系<>能不能连接,主机名是否匹配.登陆使用的用户名和密码是否正确.mysql验证用户需要检查3项值:用户名.密码和主机来源(user.password. ...

  2. 自定义tt文本模板实现MySql指数据库中生成实体类

    自定义tt文本模板实现MySql指数据库中生成实体类 1.在项目中依次点击“添加”/“新建项”,选择“文本模板”,输入名称后点击添加. 2.在Base.tt中添加如下代码. <#@ templa ...

  3. PHP中将数据库中的数据显示在网页

    最近没事把以前的东西感觉还可以的又简单的看了以下,因为还在学习新的东西,所以,发表的博客也不是很多,也许是有感而发吧. 这次讲的是mysql数据库中的数据使用php如何显示在网页中.首先,先建好自己的 ...

  4. 优化MD5和IP在(MySQL)数据库中的存储

    1.MD5在MySQL数据库中的存储 用CHAR(32)来存储MD5值是一个常见的技巧.如果你的应用程序使用VARCHAR(32),则对每个值得字符串长度都需要花费额外的不 必要的开销.这个十六进制的 ...

  5. MySql 查询数据库中所有表名

    查询数据库中所有表名select table_name from information_schema.tables where table_schema='csdb' and table_type= ...

  6. mysql查询数据库中包含某字段(列名)的所有表

    SELECT TABLE_NAME '表名',TABLE_SCHEMA '数据库名',ORDINAL_POSITION '顺序',COLUMN_NAME '字段',DATA_TYPE '类型' ,CH ...

  7. mysql 从数据库中获取多条记录,二维展示数据

    展示要求: 客户/日期 2017-10-16 1017-10-17 2017-10-18 客户1       客户2       数据库中数据: 解决办法: 1.新建一个实体类:客户名称.客户数据(A ...

  8. MySql 查询数据库中所有表名以及对比分布式库中字段和表的不同

    查询数据库中所有表名select table_name from information_schema.tables where table_schema='数据库名' and table_type= ...

  9. php mysql替换数据库中出现过的所有域名实现办法 (原)

    2019-10-12备注: 数据量稍微有些大且前期数据库建设相当完善的可以看一下这边的方法,数据量小或者数据库建设不完善的可以参考这篇文章,前两天看的,没自己试,有需要可以试试  https://ww ...

随机推荐

  1. JAVA服务器搭建之问题总结

    负责维护公司产品的web服务器搭建与维护,最近遇到一下状况,今天在这里简单总结一下,希望对于刚刚一些刚入行的小伙伴有所帮助,避免再走弯路. 第一点:Tomcat内存设置: 一.常见的Java内存溢出有 ...

  2. 【开源】OSharp3.3框架解说系列:重新开源及3.3版本新特性

    OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...

  3. Javascript之一切皆为对象1

    在javascript的世界里,有这么一句话,一切皆为对象. 但是这个对象,应该怎么理解呢? OMG,难道值类型也是对象?!! 当然,不是. 准确地讲是对于“引用类型”而言. 那,在JavaScrip ...

  4. 回忆:#define的用法

    ANSI C规定:#前可以有空格或者tab,#和指令其余部分之间也可以有空格,可以出现在任何地方,作用域从定义处到文件结尾. 因为预处理开始前,系统会删除反斜线和换行符的组合,故可以把指令扩展到几个物 ...

  5. my SQL下载安装,环境配置,以及密码忘记的解决,以及navicat for mysql下载,安装,测试连接

    一.下载 在百度上搜索"mysql-5.6.24-winx64下载" 二.安装 选择安装路径,我的路径“C:\Soft\mysql-5.6.24-winx64” 三.环境配置 计算 ...

  6. 探秘Tomcat——一个简易的Servlet容器

    即便再简陋的服务器也是服务器,今天就来循着书本的第二章来看看如何实现一个servlet容器. 背景知识 既然说到servlet容器这个名词,我们首先要了解它到底是什么. servlet 相比你或多或少 ...

  7. iOS的一些面试题分析总结(1)

    本篇主要说一下多线程的东西,因为东西比较多,所以直接拿出一整个篇幅来说它了. 3. 多线程的底层实现? 1.先说一下什么是进程,什么是线程.什么是多线程. 进程是资源分配的单位,线程是调度运行的基本单 ...

  8. 在Mac下运行ASP.NET Core应用程序

    在Mac下运行ASP.NET Core应用程序 通过参照.NET Core相关官方文档,在我的Mac电脑上用Visual Studio Code创建了我的第一个ASP.NET应用. 开发环境搭建 首先 ...

  9. Nancy之Cache的简单使用

    一.前言 说起缓存,或许大家都不陌生,甚至是天天都在用!当然缓存也被分的越来越细,页面缓存.数据缓存.文件缓存等等. 其实呢,我个人觉得,主要还是两大类:数据的缓存和页面的缓存.数据缓存的话就包括所有 ...

  10. [WCF编程]2.SOA概述

    一.SOA简介 1. SOA(面向服务架构)既是一种编程方式,也是软件开发的一种架构方法.根据这种架构方法,应用程序是由具有一定行为(称为服务)的功能单元组成的. 2. SOA的基本思想是构建一个粗粒 ...