您好,登錄后才能下訂單哦!
請(qǐng)求方式GET和POST的簡(jiǎn)單分別:
get方式是把參數(shù)附加到URL地址后面,如:
http://localhost:8080/loginServlet.html?username=123&password=456
post是將請(qǐng)求參數(shù)放到請(qǐng)求體中,以流的方式傳到服務(wù)器,另外上傳文件時(shí),一定是post方式
下面的代碼是用post方式模擬用戶(hù)登錄
package com.yuanlp.qqloginpost; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class MainActivity extends AppCompatActivity { private static final int LOAD_SUCCESS =1 ; private static final int LOAD_ERROR =2 ; private EditText mQqNum; private EditText mQqPwd; private CheckBox mCb_rember; private Button sub; private String mQq; private String mPwd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mQqNum= (EditText) findViewById(R.id.et_qqNum); mQqPwd = (EditText) findViewById(R.id.et_pwd); mCb_rember = (CheckBox) findViewById(R.id.cb_rember); sub = (Button) findViewById(R.id.bt_sub); } /** * 按鈕對(duì)應(yīng)的點(diǎn)擊方法 * @param view */ public void login(View view){ //Toast.makeText(this,"點(diǎn)擊了提交",Toast.LENGTH_SHORT).show(); mQq = mQqNum.getText().toString().trim(); mPwd = mQqPwd.getText().toString().trim(); //mCb_rember.getText().toString().trim(); if (TextUtils.isEmpty(mQq)||TextUtils.isEmpty(mPwd)){ Toast.makeText(this,"QQ號(hào)碼或者密碼為空",Toast.LENGTH_SHORT).show(); return; } //這里設(shè)置按鈕不能點(diǎn),應(yīng)為一直點(diǎn),就一直發(fā)送請(qǐng)求,會(huì)造成一直請(qǐng)求數(shù)據(jù) sub.setEnabled(false); /** * 點(diǎn)擊按鈕事件,在主線程中開(kāi)啟一個(gè)子線程進(jìn)行網(wǎng)絡(luò)請(qǐng)求 * (因?yàn)樵?.0只有不支持主線程進(jìn)行網(wǎng)絡(luò)請(qǐng)求,所以一般情況下,建議另開(kāi)啟子線程進(jìn)行網(wǎng)絡(luò)請(qǐng)求等耗時(shí)操作)。 */ //請(qǐng)求網(wǎng)絡(luò) new Thread(){ @Override public void run() { try { Thread.sleep(5000); String path="http://192.168.1.111:10010/aos/pdaLogin.jhtml"; URL url = new URL(path); //打開(kāi)httpurlconnection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); //設(shè)置get方式獲取數(shù)據(jù) conn.setConnectTimeout(5000); //設(shè)置連接超時(shí)時(shí)間5秒 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //如果設(shè)置方式為post,則必須制定該屬性 //將數(shù)據(jù)進(jìn)行編碼,然后會(huì)自動(dòng)的將該數(shù)據(jù)放到post中傳到后臺(tái) String data="username="+ URLEncoder.encode(mQq,"utf-8")+"&password="+URLEncoder.encode(mPwd,"utf-8"); //指定長(zhǎng)度 conn.setRequestProperty("Content-length",String.valueOf(data.length())); /** * post是以流的方式寫(xiě)給服務(wù)器 */ conn.setDoOutput(true); //指定輸出模式 conn.getOutputStream().write(data.getBytes()); //將要傳遞的數(shù)據(jù)寫(xiě)入輸出流 int code = conn.getResponseCode(); // 獲取response狀態(tài),200表示成功獲取資源,404表示資源不存在 if (code==200){ InputStream is=conn.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); StringBuffer sb=new StringBuffer(); String len=null; while((len=br.readLine())!=null){ sb.append(len); } String result=sb.toString(); /** * 這里就不用handler方式來(lái)處理子線程的數(shù)據(jù)了 */ runToastAnyThread(result); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } /** * 在任何線程中都可以彈出吐司 * @param result */ private void runToastAnyThread(final String result) { /** * 在這個(gè)run方法里寫(xiě)的任何方法都是在UI線程中執(zhí)行 */ runOnUiThread(new Runnable() { @Override public void run() { JSONObject jsonObject=null; try { jsonObject=new JSONObject(result); String code=jsonObject.get("appcode").toString(); if ("0".equals(code)){ Toast.makeText(getApplicationContext(),"登錄失敗",Toast.LENGTH_SHORT).show(); }else if("1".equals(code)){ Toast.makeText(getApplicationContext(),"登錄成功",Toast.LENGTH_SHORT).show(); } sub.setEnabled(true); } catch (JSONException e) { e.printStackTrace(); } } }); } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。