In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Simple differences between request methods GET and POST:
The get method is to append parameters to the URL address, such as:
Http://localhost:8080/loginServlet.html?username=123&password=456
Post is to put the request parameters into the request body and stream them to the server. In addition, when uploading files, it must be post.
The following code simulates user login in post mode
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) * @ param view * / public void login (View view) {/ / Toast.makeText (this, "Click submit", 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 number or password is empty", Toast.LENGTH_SHORT) .show (); return } / / the button set here cannot be clicked, it should be a constant point, and the request will be sent all the time, which will cause the data to be sub.setEnabled (false) all the time. / * Click the button event to open a child thread in the main thread to make network requests * (since only the main thread is not supported for network requests in 4.0, in general, it is recommended to open another child thread to perform time-consuming operations such as network requests). * / / request network 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) / / Open httpurlconnection HttpURLConnection conn = (HttpURLConnection) url.openConnection (); conn.setRequestMethod ("POST"); / / set get method to obtain data conn.setConnectTimeout (5000) / / set the connection timeout for 5 seconds conn.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded") / / if the setting method is post, you must set this attribute / / encode the data, and then automatically put the data into post and transfer it to the background String data= "username=" + URLEncoder.encode (mQq, "utf-8") + "& password=" + URLEncoder.encode (mPwd, "utf-8") / / specify the length conn.setRequestProperty ("Content-length", String.valueOf (data.length (); / * post is written to the server * / conn.setDoOutput (true) as a stream / / specify the output mode conn.getOutputStream () .write (data.getBytes ()); / / write the data to be passed to the output stream int code = conn.getResponseCode () / / get the response status. 200indicates that the resource was acquired successfully, and 404indicates that the resource does not exist: 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 () / * here we don't use handler to process the data of child threads * / runToastAnyThread (result);}} catch (Exception e) {e.printStackTrace () }. Start () } / * Toast can be popped up in any thread * @ param result * / private void runToastAnyThread (final String result) {/ * any method written in this run method is executed in the UI thread * / 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 (), login failed, Toast.LENGTH_SHORT) .show () } else if ("1" .equals (code)) {Toast.makeText (getApplicationContext (), "login successful", Toast.LENGTH_SHORT). Show ();} sub.setEnabled (true);} catch (JSONException e) {e.printStackTrace () );}}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.