我想把下面的button监听事件里的代码封装成一个类随时调用,请问怎么封装,谢谢!
Android通过PHP连接MySQL(传值查询)(2012-05-28 18:19:01)转载▼标签: 杂谈 分类: Java和 Android通过PHP连接MySQL(读取) 类似,界面不变,把 AndroidTestActivity.java 修改为:
select.php
<?php
mysql_connect("127.0.0.1","root","123456");
mysql_query("SET NAMES utf8");
mysql_select_db("test");
$sql=mysql_query("select * from teacher where id='".$_REQUEST['id']."' and name='".$_REQUEST['name']."'");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>
package com.knight.android.MySQL;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Android_MySQLActivity extends Activity {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText tv = (EditText) findViewById(R.id.editView);
ArrayList nameValuePairs = new ArrayList();
nameValuePairs.add(new BasicNameValuePair("id","3"));
nameValuePairs.add(new BasicNameValuePair("name","Guo"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://175.186.54.10/select.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
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());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
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();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//paring data
int ct_id;
String ct_name;
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");
ct_name=json_data.getString("name");
tv.append(ct_name+" \n");
}
}catch(JSONException e1){
// Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
});
}
} --------------------编程问答--------------------
--------------------编程问答-------------------- 不用新建一个类吗?就直接这样?你这是封装么?
public class Android_MySQLActivity extends Activity {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new ButtonOnClickListener());
}
private class ButtonOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText tv = (EditText) findViewById(R.id.editView);
ArrayList nameValuePairs = new ArrayList();
nameValuePairs.add(new BasicNameValuePair("id","3"));
nameValuePairs.add(new BasicNameValuePair("name","Guo"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://175.186.54.10/select.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
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());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
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();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//paring data
int ct_id;
String ct_name;
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");
ct_name=json_data.getString("name");
tv.append(ct_name+" \n");
}
}catch(JSONException e1){
// Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}
}
--------------------编程问答--------------------
写成这样了,还要我怎么说,不能自己把ButtonOnClickListener类分割出来? --------------------编程问答-------------------- 分割出来的类找不到EditText,啊 --------------------编程问答-------------------- EditText tv = (EditText) findViewById(R.id.editView);
能写到oncreate函数里不?
补充:Java , Java相关