此代码用于通过发布两个数据 num、db 并获得响应来从 php 服务器获取详细信息。公共类 MainActivity 扩展 Activity {
Button b;
TextView num;
TextView dob;
String db;
String x;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button) findViewById(R.id.button1);
num=(TextView) findViewById(R.id.editText1);
dob=(TextView) findViewById(R.id.editText2);
x=(String) num.getText();
db=(String) dob.getText();
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
submit();
}
});
}
submit fn 用于将两个值 num,db 发布到服务器以获取响应。
public String submit(){
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("registerno", x ));
postParameters.add(new BasicNameValuePair("dob", db ));
String response = "";
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.pnrinbox.com/premfin.php");
try
{
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null)
{
response += s;
}
Log.d( "After call, response: " , " " + response);
}
catch (Exception e)
{
System.out.println(e.toString());
}
return response;
}
将两个数据项发布到 php 服务器并取回数据。