在這我們首先了解Android客戶端向服務器提交數據的底層做法。android/56789.html">get、post兩種方法提交數據,下面我們用示例了解get以及post方式。
需要在布局文件中增加兩個個EditText控件和兩個登錄的Button控件。其中一個Button是使用get方式提交數據,一個是使用post提交數據。
<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方式提交數據
需要寫一個異步任務類繼承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提交數據的點擊事件 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) { //獲取參數的值 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; //判斷提交數據的類型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方式提交數據
post和get一樣需要寫一個異步任務類繼承AsyncTask,重寫它的兩個方法。但是post需要設置Content-Length、Content-Type以及對外輸出數據。而且請求的路徑不同,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提交數據的點擊事件 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) { //獲取參數的值 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; //判斷提交數據的類型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"); //設置允許對外輸出數據 httpURLConnection.setDoOutput(true); //將用戶名密碼以流的形式對外輸出 httpURLConnection.getOutputStream().write(s.getBytes()); httpURLConnection.setRequestMethod("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); } } } 另外,需要增加網絡權限:
<uses-permission android:name="android.permission.INTERNET" />
三、總結
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方式需要請求內容,而get方式的相應內容在URL地址中。
4、post方式與get方式的請求時所攜帶的內容大小不同。
get:1k。
post:理論上是無限制的,相對于而言post適合傳大量數據。
5、get方式的數據直接顯示在URL地址中,這是不安全的;而post方式不會,安全性相對于比較高。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答