android 通过访问 php 接受 or 传送数据

时间:2023-03-09 13:05:57
android  通过访问  php  接受  or  传送数据

先说传送数据,可以在 利用 php 代替传送,直接把 访问的url加上 xxx.php?informatin=xxxxxx 就行了

接收的看代码吧,详细注释。

首先是 我自己定义的php 文件

 <?php
header("Content-Type: text/html; charset=utf8"); $DataBase=$_REQUEST["DB"];//照应我java文件里面设置的DB
//$DataBase = "test";
//$col_name="content";
//$Order = "select * from user";
$Order=$_REQUEST["Order"];//照应Order //分别弄 创建、查询、插入、删除、更新的部分 $link=mysql_connect("localhost","root","");
mysql_query("SET NAMES 'utf8'",$link); //经验总结,使用mysql设置页面编码,最好等链接了,再设置,意思是在连库函数后面使用 if(!$link){
echo "connect_dataBase_wrong";exit();
}
if(!mysql_select_db($DataBase,$link)){
exit("select_db_wrong");
}
if(!$selec=mysql_query($Order,$link)){
exit("select_table_wrong");
}
$info=array();
$i=0;
if(mysql_num_rows($selec)){
while($row=mysql_fetch_assoc($selec)){
$data[] = $row;
}
//echo $row[$col_name]."</br>";
print(json_encode($data));
//echo $info[$i]."</br>";
//$i++;
mysql_close();
}
 package com.example.administrator.lianxi;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList; /*Powered By LinGH-2015.2.16*/
/*
提示,在使用下面类的,android主页,一定要在onCreate函数里面的super.onCreate(savedInstanceState);之前加上下面两句,对应的头文件是 import android.os.StrictMode; 不加入会抛出无法联网的异常,因为在android 2.3之前是可以直接写要联网的代码的,之后就要另建线程了,具体请百度。 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build()); 还有,请在虚拟机上运行,别运行java程序,会抛错误异常的,这里有android的包 还一个是,请在AndroidManifest xml 页中,加入 联网的 pression
<uses-permission android:name="android.permission.INTERNET" />
*/
public class sql {
private String[] dataForTitle = new String[100];//定义一个用来放Listview 标题的 字符串数组,每个字符串长度为100字节
private String[] dataForContent = new String[100];//定义一个用来放Listview 内容的 字符串数组,每个字符串长度为100字节
private int rowNum = 0;//这个整型是用来保存数据表行数,用来返回的
private String result = "";//中间变量 public ArrayList<NameValuePair> init(String order,String db) {//这个函数用来初始化数组列表ArrayList
ArrayList<NameValuePair> name = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器
name.add(new BasicNameValuePair("Order",order));//这里设置php文件接收的Order,例如 $_REQUEST["Order"],根据你的php自己定义的来写
name.add(new BasicNameValuePair("DB",db));//上面我定义了一个mysql命令,这里是数据库名字,两个由参数传入,增加了灵活性
return name;//返回设置好了容器
} public String[] MySql_And_Get_colName(String url,String order,String db,String colName,String colName_1) {
//这条函数第一个参数是:你的终端php链接;第二个参数是:你要执行的数据库命令,根据你的php设置而定;
// 第三个参数:是要使用的数据库名字;第四个参数是:自定义的,你自己可以改,我这里是用来标记数据表的列名,和第5个参数一样,还能更多标记,自己设置;
InputStream GetContentFromDb = null;//定义一个保存输入流的变量
try {
HttpClient http = new DefaultHttpClient();//开启http服务
HttpPost post = new HttpPost(url);//传入url,初始化要post数据的url
post.setEntity(new UrlEncodedFormEntity(init(order,db)));//这里发送数据,看到init()函数的调用没
HttpResponse response = http.execute(post);//这里才正真地进行访问,带着上面设置的数据
HttpEntity responseFromDb = response.getEntity();//接受返回的实体
GetContentFromDb = responseFromDb.getContent();//接受实体内容,并保存到输入流对象中
} catch (Exception e) {
dataForTitle[0]=e.toString();
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(GetContentFromDb,"UTF-8"),8);
//上面的这句作用是把输入流里面的内容进行编码,第二个最好设置UTF-8,要和你的mysql表的一样,如果用iso-8859-1可能会抛出乱码错误
StringBuilder info = new StringBuilder();//定义字符容器
String line = null;//用来保存提取出的每行数据
while((line = reader.readLine())!=null){//保证读到的每行数据不为null
info.append(line+"\n");//每行相加
}
GetContentFromDb.close();//关闭
result=info.toString();//数据转化
}catch (Exception e){
dataForTitle[0]=e.toString();
}
try{
JSONArray jArray = new JSONArray(result);//把数据php的json数据放回到这里,记住,你php最后输出的一定要是json数据,否则,这里会抛出异常
if(jArray.length()>0) {//是否有数据
rowNum = jArray.length();//获取行数,并保存
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);//逐行获取接受回来的json数据
dataForTitle[i] = json_data.getString(colName);//将所想要获取的列数据存入字符串数组,我这里是title
dataForContent[i] = json_data.getString(colName_1);//这里是content
// System.out.println(data[i]);
}
}
}catch (Exception e){
dataForTitle[0]=e.toString();
}
return dataForTitle;
}
public String[] getColName_1(){
return dataForContent; //返回content
}
public int getRowNum(){
return rowNum; //返回行数
}
}