1.PHP端读取MYSQL
<?php
mysql_connect("222.31.76.240:3306","root","");
mysql_select_db("cuc_music");
mysql_query("SET NAMES 'utf8'"); //這句一定要加,将数据库、PHP、Android客户端三个部分的编码统一
$sql=mysql_query("SELECT * FROM cuc_music.song WHERE songName = '".$_REQUEST['songName']."' limit 100");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
//print($_REQUEST['songName']);
mysql_close();
?>
2.Android端向PHP发消息
public class MainActivity extends Activity implements
android.view.View.OnClickListener {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb = null;
private Button btn_search;
private EditText et_search;
private TextView tv;
// MyThread myThread;
// public Thread MysqlThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_search = (Button) findViewById(R.id.btn_search);
et_search = (EditText) findViewById(R.id.et_search);
tv = (TextView) findViewById(R.id.textView1);
btn_search.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.btn_search) {
//查询线程
Thread thread = new Thread(new MysqlThread());
thread.start();
return;
}
}
/*查询线程类*/
public class MysqlThread implements Runnable {
@Override
public void run() {
Log.d("mysqlThread", "mysqlThread");
List mysqlQueryPara = new ArrayList();
mysqlQueryPara.add(new BasicNameValuePair("songName",et_search.getText().toString()));
// nameValuePairs.add(new BasicNameValuePair("name","Guo"));
/*向PHP发消息,得到json结果的回复*/
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://222.31.81.163/testmysql.php");
httppost.setEntity(new UrlEncodedFormEntity(mysqlQueryPara,HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
/*将收到的json回复转换成String类型*/
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "utf8"));//这句非常重要,需要将MYSQL/php/android编码格式统一起来
sb = new StringBuilder();
sb.append(reader.readLine() + "n");
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
result = sb.toString();
// Log.e("result", result);
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
/*以下为解析json部分,将解析json后的数据打包以message的形式传个Handler处理*/
int ct_id;
ArrayList<String> list = new ArrayList<String>();
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
// ct_id = json_data.getInt("id");
list.add(json_data.getString("songName"));
// tv.append(ct_name + " n");
Log.d("song", json_data.getString("songName"));
}
} catch (JSONException e1) {
// Toast.makeText(getBaseContext(), "No City Found"
// ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
// 获取一个Message对象,设置what为1
Message msg = new Message();
msg.what = 1;
Bundle data = new Bundle();
data.putSerializable("list", list);
msg.setData(data);
// 发送这个消息到消息队列中
handler.sendMessage(msg);
}
}
/*消息处理*/
Handler handler = new Handler() {
public void handleMessage(Message msg) {
// Integer.parseInt("good");
switch (msg.what) {
/*处理收到的消息,前台页面更新*/
case 1:
// Log.d("handleMessage", "handleMessage");
Bundle data = msg.getData();
ArrayList list = (ArrayList) data.getSerializable("list");
for (int i = 0; i < list.size(); i++) {
tv.append((CharSequence) list.get(i));
// System.out.println();
}
break;
default:
break;
}
super.handleMessage(msg);
}
};
}