安卓下使用GET方式向伺服器端提交資料

來源:互聯網
上載者:User

標籤:安卓   伺服器   用戶端   提交資料   

從用戶端提交資料給伺服器端:

 

  使用get方式向伺服器端提交資料,把參數組拼到了url地址的後面:

http://192.168.0.10:8080/web/servlet/LoginServlet

?username=123&password=123445

 

源碼板塊:

package com.itheima.qqlogin;

 

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

 

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.text.TextUtils;

import android.view.View;

import android.widget.EditText;

import android.widget.Toast;

 

import com.itheima.htmlview.utils.StreamTools;

 

public class MainActivity extends Activity {

 

private static final int ERROR = 0;

private static final int FAILED = 1;

private static final int SUCCESS = 2;

private EditText et_qq;

private EditText et_pwd;


//1、在主線程中建立一個handler的成員變數:

private Handler handler = new Handler(){

public void handleMessage(Message msg) {

int what = msg.what;

switch (what) {

case 0:

Toast.makeText(MainActivity.this, "訪問網路失敗", 0).show();

break;

 

case 1:

Toast.makeText(MainActivity.this, "服務返回資料失敗", 0).show();

break;

case 2:

String result = (String) msg.obj;

Toast.makeText(MainActivity.this, result, 0).show();

break;

}



};

};

 

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

 

et_qq = (EditText) findViewById(R.id.et_qq);

et_pwd = (EditText) findViewById(R.id.et_pwd);

}

 

public void login(View v) {

final String qq = et_qq.getText().toString().trim();

 

final String pwd = et_pwd.getText().toString().trim();

 

if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)) {

Toast.makeText(this, "qq號碼或者密碼不可為空", 0).show();

return;

} else {

new Thread(){

public void run() {

try {

// 1、建立URL對象、開啟HTTP類型的串連;

 

String path = "http://192.168.0.10:8080/web/servlet/LoginServlet?username="+qq+"&password="+pwd;

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection) url

.openConnection();

 

// 2、佈建要求頭資訊:GET;

 

conn.setRequestMethod("GET");

// 設定串連的逾時時間

conn.setConnectTimeout(3000);

// conn.setRequestProperty("Accept-Language", "zh-CN");

 

// 3、擷取伺服器端返回的響應資料,二進位的輸入資料流;

 

int code = conn.getResponseCode();

if (code == 200) {

InputStream is = conn.getInputStream();

String result = StreamTools.readStream(is);


Message msg = Message.obtain();

//訊息碼,用來區分是在什麼地方發送的訊息

msg.what = SUCCESS;

msg.obj = result;

handler.sendMessage(msg);

}else{

//服務返回資料失敗

Message msg = Message.obtain();

//msg.obj = "服務返回資料失敗";

msg.what = FAILED;

handler.sendMessage(msg);

}

} catch (Exception e) {

e.printStackTrace();


//訪問網路失敗

Message msg = Message.obtain();

//msg.obj = "訪問網路失敗";

msg.what = ERROR;

handler.sendMessage(msg);

}

};

}.start();

}

}

 

}


本文出自 “想富賣切糕” 部落格,請務必保留此出處http://dkpzjl.blog.51cto.com/9545260/1569869

安卓下使用GET方式向伺服器端提交資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.