android练习:使用get方法发送请求

来源:互联网 发布:淘宝网那个表示 编辑:程序博客网 时间:2024/06/05 18:59

今天想写一个有关查询的demo,于是就学习了一下有关android访问网络数据方面的内容。在android中很多情况下需要使用服务器上的资源就要使用http协议进行访问。我们都知道http协议访问互联网有两种方式,一种是get,第二种是post方法,这里我只用到了get方法,传给服务器一个值,然后返回参数。

这里是我写的代码。

首先需要在配置文件中添加联网权限,这个千万记住,我在学习初期老是忘了这部工作。
<uses-permission Android:name="android.permission.INTERNET"></uses-permission>
下面是主要代码
 protected void onCreate(Bundle savedInstanceState)
    {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mNameText = (EditText) findViewById(R.id.name);
        mAgeText = (EditText) findViewById(R.id.age);
        textView_Get = (TextView) findViewById(R.id.TextView_Get);
        button1 = (Button)findViewById(R.id.button1);
        
        
        button1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {


new Thread(new Runnable() {

@Override
public void run() {


String id = mNameText.getText().toString();
           String name = mAgeText.getText().toString();
       
httpUrl =  baseURL + "?method=fetch&type=TIMES&student_id=" + id + "&student_name=" + name;
         
HttpGet httpGet = new HttpGet(httpUrl);  
                try {
httpResponse = new DefaultHttpClient().execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200)  
                {  
                      
                    result = EntityUtils.toString(httpResponse  
                            .getEntity());  
                   
                    Log.e("info", result);
               
                    runOnUiThread(new Runnable() {

@Override
public void run() {

textView_Get.setText(result.replaceAll("times", "次数").replaceAll("query", "查询结果"));


//
//
// }
}
});
                }  
} catch (ClientProtocolException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}  
}
}).start();
 
                 
}
});
    }
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {  
        if (keyCode == KeyEvent.KEYCODE_BACK )  
        {  
            // 创建退出对话框  
            AlertDialog isExit = new AlertDialog.Builder(this).create();  
            // 设置对话框标题  
            isExit.setTitle("系统提示");  
            // 设置对话框消息  
            isExit.setMessage("确定要退出吗");  
            // 添加选择按钮并注册监听  
            isExit.setButton("确定", listener);  
            isExit.setButton2("取消", listener);  
            // 显示对话框  
            isExit.show();  
  
        }  
          
        return false;  
          
    }  
    /**监听对话框里面的button点击事件*/  
    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()  
    {  
        public void onClick(DialogInterface dialog, int which)  
        {  
            switch (which)  
            {  
            case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序  
                finish();  
                break;  
            case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框  
                break;  
            default:  
                break;  
            }  
        }  
    };    



0 0