詳解Android:向服務器提供數(shù)據(jù)之get、post方式
在這我們首先了解Android客戶端向服務器提交數(shù)據(jù)的底層做法。get、post兩種方法提交數(shù)據(jù),下面我們用示例了解get以及post方式。
需要在布局文件中增加兩個個EditText控件和兩個登錄的Button控件。其中一個Button是使用get方式提交數(shù)據(jù),一個是使用post提交數(shù)據(jù)。
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_main_name"
android:hint="請輸入用戶名"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_main_pwd"
android:hint="請輸入用戶名"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄GET"
android:onClick="getdata"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄POST"
android:onClick="postdata"
/>
一、使用get方式提交數(shù)據(jù)
需要寫一個異步任務類繼承AsyncTask,重寫它的兩個方法。代碼如下:
public class MainActivity extends AppCompatActivity {
private EditText et_main_name;
private EditText et_main_pwd;
private HttpURLConnection httpURLConnection;
private URL url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_name = (EditText) findViewById(R.id.et_main_name);
et_main_pwd = (EditText) findViewById(R.id.et_main_pwd);
}
//獲取GET提交數(shù)據(jù)的點擊事件
public void getdata(View view){
String name=et_main_name.getText().toString();
String pwds=et_main_pwd.getText().toString();
String path="http://192.168.43.143:8080/login/login.xhtml";
new myTask().execute(name,pwds,path,"GET");
}
//寫一個異步任務類繼承AsyncTask,重寫它的兩個方法
class myTask extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
//獲取參數(shù)的值
String name = params[0].toString();
String pwds = params[1].toString();
String path = params[2].toString();
String type = params[3].toString();
String s = "uname=" + name + "&pwd=" + pwds;
//判斷提交數(shù)據(jù)的類型1、get 2、post
try {
if ("GET".equals(type)) {//用get方式提交
path = path + "?" + s;
url = new URL(path);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
} else if ("POST".equals(type)) {//用post方式提交
}
httpURLConnection.setConnectTimeout(5000);
if (httpURLConnection.getResponseCode() == 200) {
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String result = bufferedReader.readLine();
return result;
}
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
String str= (String) o;
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
super.onPostExecute(o);
}
}
}
二、使用post方式提交數(shù)據(jù)
post和get一樣需要寫一個異步任務類繼承AsyncTask,重寫它的兩個方法。但是post需要設置Content-Length、Content-Type以及對外輸出數(shù)據(jù)。而且請求的路徑不同,post相對于比get安全。代碼如下:
public class MainActivity extends AppCompatActivity {
private EditText et_main_name;
private EditText et_main_pwd;
private HttpURLConnection httpURLConnection;
private URL url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_name = (EditText) findViewById(R.id.et_main_name);
et_main_pwd = (EditText) findViewById(R.id.et_main_pwd);
}
//獲取POST提交數(shù)據(jù)的點擊事件
public void postdata(View view){
String name=et_main_name.getText().toString();
String pwds=et_main_pwd.getText().toString();
String path="http://192.168.43.143:8080/login/login.xhtml";
new myTask().execute(name,pwds,path,"POST");
}
//寫一個異步任務類繼承AsyncTask,重寫它的兩個方法
class myTask extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
//獲取參數(shù)的值
String name = params[0].toString();
String pwds = params[1].toString();
String path = params[2].toString();
String type = params[3].toString();
String s = "uname=" + name + "&pwd=" + pwds;
//判斷提交數(shù)據(jù)的類型1、get 2、post
try {
if ("GET".equals(type)) {//用get方式提交
} else if ("POST".equals(type)) {//用post方式提交
url = new URL(path);//得到請求的路徑
httpURLConnection = (HttpURLConnection) url.openConnection();
//設置Content-Length Content-Type
httpURLConnection.setRequestProperty("Content-Length", s.length() + "");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//設置允許對外輸出數(shù)據(jù)
httpURLConnection.setDoOutput(true);
//將用戶名密碼以流的形式對外輸出
httpURLConnection.getOutputStream().write(s.getBytes());
httpURLConnection.setRequestMethod("POST");//數(shù)據(jù)提交的類型
}
httpURLConnection.setConnectTimeout(5000);
if (httpURLConnection.getResponseCode() == 200) {//判斷響應碼
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String result = bufferedReader.readLine();
return result;
}
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
String str= (String) o;
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
super.onPostExecute(o);
}
}
}
另外,需要增加網(wǎng)絡權(quán)限:
<uses-permission android:name="android.permission.INTERNET" />
三、總結(jié)
1、get方式與post方式請求的路徑(URL地址)不同。
2、post方式需要對請求頭的設置。
//設置Content-Length Content-Type
httpURLConnection.setRequestProperty("Content-Length", s.length() + "");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
3、post方式需要請求內(nèi)容,而get方式的相應內(nèi)容在URL地址中。
4、post方式與get方式的請求時所攜帶的內(nèi)容大小不同。
get:1k。
post:理論上是無限制的,相對于而言post適合傳大量數(shù)據(jù)。
5、get方式的數(shù)據(jù)直接顯示在URL地址中,這是不安全的;而post方式不會,安全性相對于比較高。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- android AsynTask處理返回數(shù)據(jù)和AsynTask使用get,post請求
- Android中post和get的提交方式【三種】
- Android中使用OkHttp包處理HTTP的get和post請求的方法
- Android中使用HttpURLConnection實現(xiàn)GET POST JSON數(shù)據(jù)與下載圖片
- android平臺HttpGet、HttpPost請求實例
- android使用url connection示例(get和post數(shù)據(jù)獲取返回數(shù)據(jù))
- Android發(fā)送GET與POST請求的DEMO詳解
- android之HttpPost&HttpGet使用方法介紹
- Android HttpClient GET或者POST請求基本使用方法
- 安卓GET與POST網(wǎng)絡請求的三種方式
相關文章
Android 百度地圖定位實現(xiàn)仿釘釘簽到打卡功能的完整代碼
這篇文章主要介紹了Android 百度地圖定位實現(xiàn)仿釘釘簽到打卡功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法
這篇文章主要介紹了Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法,實例分析了Android操作SQLite數(shù)據(jù)庫的相關技巧,需要的朋友可以參考下2015-06-06
Flutter開發(fā)之對角棋游戲?qū)崿F(xiàn)實例詳解
這篇文章主要為大家介紹了Flutter開發(fā)之對角棋游戲?qū)崿F(xiàn)實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android編程實現(xiàn)XML解析與保存的三種方法詳解
這篇文章主要介紹了Android編程實現(xiàn)XML解析與保存的三種方法,結(jié)合實例形式詳細分析了Android實現(xiàn)xml解析的SAX、DOM、PULL三種方法的相關操作技巧,需要的朋友可以參考下2017-08-08
如何玩轉(zhuǎn)Android矢量圖VectorDrawable
這篇文章主要教大家如何玩轉(zhuǎn)Android矢量圖VectorDrawable,對矢量圖感興趣的小伙伴們可以參考一下2016-05-05
Android編程開發(fā)之EditText中inputType屬性小結(jié)
這篇文章主要介紹了Android編程開發(fā)之EditText中inputType屬性用法,分析說明了Android中EditText的inputType屬性具體含義與使用技巧,需要的朋友可以參考下2016-01-01
Android Studio中導入JNI生成的.so庫的實現(xiàn)方法
這篇文章主要介紹了Android Studio中導入JNI生成的.so庫的實現(xiàn)方法的相關資料,這里不僅提供實現(xiàn)方案并提供了實現(xiàn)的方法,需要的朋友可以參考下2017-07-07
Android?Framework原理Binder驅(qū)動源碼解析
這篇文章主要為大家介紹了Android?Framework原理Binder驅(qū)動源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01

