Android--android客户端查询mysql数据库
来源:互联网 发布:程序员用浏览器 编辑:程序博客网 时间:2024/06/05 15:39
//注意 由于安卓4.0版本以后 禁止在主线程里 直接操作网络 故该demo只能在android2.3以下版本运行,如需在高版本里运行 新建个副线程即可!!
package com.example.jsontransdata;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;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.ContentEncodingHttpClient;import org.apache.http.impl.client.DefaultHttpClient;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {Button btn;EditText edt;String line=""; String content="";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn=(Button)findViewById(R.id.btn1);edt=(EditText)findViewById(R.id.edt);btn.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View v) {HttpGet httpget=new HttpGet("http://192.168.1.105/HelloPHP/index.php");HttpClient httpclient=new DefaultHttpClient();try {HttpResponse response=httpclient.execute(httpget);HttpEntity entity=response.getEntity();InputStream input=entity.getContent();BufferedReader br=new BufferedReader(new InputStreamReader(input,"iso-8859-1"),8); while((line=br.readLine())!=null) { content+=line; } input.close(); } catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}int ct_id;String ct_name;try {JSONArray jArray=new JSONArray(content); //创建JSONAray对象 并实例化 传参String类型参数JSONObject json_data=null;//创建JSONObject 对象for(int i=0;i<jArray.length();i++){json_data=jArray.getJSONObject(i);//调用getJSONObject(i)方法先把数组里的JSONObject对象索引出来并赋给json_data对象ct_id=json_data.getInt("test_id");//再分别用getInt()和getString()方法调出对应值,完成JSON解析ct_name=json_data.getString("test_name");edt.append("数据库查询如下:\n"+ct_id+":"+ct_name+"\n");}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}PHP端代码:
<?php$link=mysql_connect("127.0.0.1","root","root");mysql_query("SET NAMES 'utf-8'");mysql_select_db("testDB",$link);$sql=mysql_query("select * from table_test",$link);while ($row=mysql_fetch_assoc($sql))$output[]=$row;print (json_encode($output));mysql_close();?>运行结果:
0 0
- Android--android客户端查询mysql数据库
- Android客户端通过PHP访问MySQL数据库
- Android客户端+JavaEE后台服务器端+Mysql数据库(社交app-心情驿站)
- android客户端向mysql数据库插入中文乱码(解决方法)
- 虚拟主机做服务器 PHP+Android客户端+MySql数据库
- scrapy爬虫++mysql数据库+tomcat服务器+android客户端项目demo
- Android通过Http连接MySQL 实现登陆/注册(数据库+服务器+客户端),android+mysql
- Android查询短信数据库
- Android查询短信数据库
- Android查询短信数据库
- android 数据库查询记录
- android 查询数据库
- android 数据库查询方法
- Android查询短信数据库
- Android数据库查询语句
- Android 数据库查询
- android查询数据库
- Android客户端程序通过Web Service实现对服务器端数据库的查询
- 重温数据结构之四——队列与堆栈
- 滚动条在div或ul等标签中出现
- 5-34 任务调度的合理性 (25分) 拓扑排序
- dztxtrxzjzjvznx
- 浅谈快速排序
- Android--android客户端查询mysql数据库
- 【剑指offer Java】面试题2:实现Singleton模式
- 私人小餐馆发生液化气罐爆
- qekqqocuuauukwe
- BMC
- Eclipse插件springsource-tool-suite的下载和安装
- 跨浏览器鼠标事件写法
- 缓存与数据库一致性保证
- 初识xhprof之安装配置