先说传送数据,可以在 利用 php 代替传送,直接把 访问的url加上 xxx.php?informatin=xxxxxx 就行了
接收的看代码吧,详细注释。
首先是 我自己定义的php 文件
1 <?php
2 header("Content-Type: text/html; charset=utf8");
3
4 $DataBase=$_REQUEST["DB"];//照应我java文件里面设置的DB
5 //$DataBase = "test";
6 //$col_name="content";
7 //$Order = "select * from user";
8 $Order=$_REQUEST["Order"];//照应Order
9
10
11
12 //分别弄 创建、查询、插入、删除、更新的部分
13
14
15
16 $link=mysql_connect("localhost","root","");
17 mysql_query("SET NAMES 'utf8'",$link); //经验总结,使用mysql设置页面编码,最好等链接了,再设置,意思是在连库函数后面使用
18
19 if(!$link){
20 echo "connect_dataBase_wrong";exit();
21 }
22 if(!mysql_select_db($DataBase,$link)){
23 exit("select_db_wrong");
24 }
25 if(!$selec=mysql_query($Order,$link)){
26 exit("select_table_wrong");
27 }
28 $info=array();
29 $i=0;
30 if(mysql_num_rows($selec)){
31 while($row=mysql_fetch_assoc($selec)){
32 $data[] = $row;
33 }
34 //echo $row[$col_name]."</br>";
35 print(json_encode($data));
36 //echo $info[$i]."</br>";
37 //$i++;
38 mysql_close();
39 }
1 package com.example.administrator.lianxi;
2 import org.apache.http.HttpEntity;
3 import org.apache.http.HttpResponse;
4 import org.apache.http.NameValuePair;
5 import org.apache.http.client.HttpClient;
6 import org.apache.http.client.entity.UrlEncodedFormEntity;
7 import org.apache.http.client.methods.HttpPost;
8 import org.apache.http.impl.client.DefaultHttpClient;
9 import org.apache.http.message.BasicNameValuePair;
10 import org.json.JSONArray;
11 import org.json.JSONObject;
12 import java.io.BufferedReader;
13 import java.io.InputStream;
14 import java.io.InputStreamReader;
15 import java.util.ArrayList;
16
17 /*Powered By LinGH-2015.2.16*/
18 /*
19 提示,在使用下面类的,android主页,一定要在onCreate函数里面的super.onCreate(savedInstanceState);之前加上下面两句,对应的头文件是
20
21 import android.os.StrictMode;
22
23 不加入会抛出无法联网的异常,因为在android 2.3之前是可以直接写要联网的代码的,之后就要另建线程了,具体请百度。
24
25 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
26 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
27
28 还有,请在虚拟机上运行,别运行java程序,会抛错误异常的,这里有android的包
29
30 还一个是,请在AndroidManifest xml 页中,加入 联网的 pression
31 <uses-permission android:name="android.permission.INTERNET" />
32 */
33 public class sql {
34 private String[] dataForTitle = new String[100];//定义一个用来放Listview 标题的 字符串数组,每个字符串长度为100字节
35 private String[] dataForContent = new String[100];//定义一个用来放Listview 内容的 字符串数组,每个字符串长度为100字节
36 private int rowNum = 0;//这个整型是用来保存数据表行数,用来返回的
37 private String result = "";//中间变量
38
39 public ArrayList<NameValuePair> init(String order,String db) {//这个函数用来初始化数组列表ArrayList
40 ArrayList<NameValuePair> name = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器
41 name.add(new BasicNameValuePair("Order",order));//这里设置php文件接收的Order,例如 $_REQUEST["Order"],根据你的php自己定义的来写
42 name.add(new BasicNameValuePair("DB",db));//上面我定义了一个mysql命令,这里是数据库名字,两个由参数传入,增加了灵活性
43 return name;//返回设置好了容器
44 }
45
46 public String[] MySql_And_Get_colName(String url,String order,String db,String colName,String colName_1) {
47 //这条函数第一个参数是:你的终端php链接;第二个参数是:你要执行的数据库命令,根据你的php设置而定;
48 // 第三个参数:是要使用的数据库名字;第四个参数是:自定义的,你自己可以改,我这里是用来标记数据表的列名,和第5个参数一样,还能更多标记,自己设置;
49 InputStream GetContentFromDb = null;//定义一个保存输入流的变量
50 try {
51 HttpClient http = new DefaultHttpClient();//开启http服务
52 HttpPost post = new HttpPost(url);//传入url,初始化要post数据的url
53 post.setEntity(new UrlEncodedFormEntity(init(order,db)));//这里发送数据,看到init()函数的调用没
54 HttpResponse response = http.execute(post);//这里才正真地进行访问,带着上面设置的数据
55 HttpEntity responseFromDb = response.getEntity();//接受返回的实体
56 GetContentFromDb = responseFromDb.getContent();//接受实体内容,并保存到输入流对象中
57 } catch (Exception e) {
58 dataForTitle[0]=e.toString();
59 }
60 try{
61 BufferedReader reader = new BufferedReader(new InputStreamReader(GetContentFromDb,"UTF-8"),8);
62 //上面的这句作用是把输入流里面的内容进行编码,第二个最好设置UTF-8,要和你的mysql表的一样,如果用iso-8859-1可能会抛出乱码错误
63 StringBuilder info = new StringBuilder();//定义字符容器
64 String line = null;//用来保存提取出的每行数据
65 while((line = reader.readLine())!=null){//保证读到的每行数据不为null
66 info.append(line+"\n");//每行相加
67 }
68 GetContentFromDb.close();//关闭
69 result=info.toString();//数据转化
70 }catch (Exception e){
71 dataForTitle[0]=e.toString();
72 }
73 try{
74 JSONArray jArray = new JSONArray(result);//把数据php的json数据放回到这里,记住,你php最后输出的一定要是json数据,否则,这里会抛出异常
75 if(jArray.length()>0) {//是否有数据
76 rowNum = jArray.length();//获取行数,并保存
77 for (int i = 0; i < jArray.length(); i++) {
78 JSONObject json_data = jArray.getJSONObject(i);//逐行获取接受回来的json数据
79 dataForTitle[i] = json_data.getString(colName);//将所想要获取的列数据存入字符串数组,我这里是title
80 dataForContent[i] = json_data.getString(colName_1);//这里是content
81 // System.out.println(data[i]);
82 }
83 }
84 }catch (Exception e){
85 dataForTitle[0]=e.toString();
86 }
87 return dataForTitle;
88 }
89 public String[] getColName_1(){
90 return dataForContent; //返回content
91 }
92 public int getRowNum(){
93 return rowNum; //返回行数
94 }
95 }