編輯:關於Android編程
本文實例為大家分享了Android使用get方式登錄的相關代碼,供大家參考,具體內容如下
工具類
package com.liunan.okhttpdemo3post.Utils; import java.io.IOException; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; /** * Created by Administrator on 2016-03-27. */ public class HttpUtils { OkHttpClient client = new OkHttpClient(); public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); public String login(String url, String json) throws IOException { //把請求的內容字符串轉換為json RequestBody body = RequestBody.create(JSON, json); //RequestBody formBody = new FormEncodingBuilder() Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = client.newCall(request).execute(); String result = response.body().string(); return result; } public String bolwingJson(String username, String password) { return "{'username':" + username + "," + "'password':" + password + "}"; // "{'username':" + username + ","+"'password':"+password+"}"; } }
Activity
package com.liunan.okhttpdemo3post; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.liunan.okhttpdemo3post.Utils.HttpUtils; import org.w3c.dom.Text; import java.io.IOException; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG ="MainActivity" ; //用戶名 private EditText mEtUsername; //密碼 private EditText mEtPwd; //登錄按鍵 private Button mBtnLogin; private TextView mTvResult; private String url ="http://192.168.1.102:8080/Login/login"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initListener(); } /** * 初始化組件 */ private void initView() { mEtUsername = (EditText) findViewById(R.id.login_et_name); mEtPwd = (EditText) findViewById(R.id.login_et_pwd); mBtnLogin = (Button) findViewById(R.id.login_btn_login); mTvResult = (TextView) findViewById(R.id.login_tv_result); } /** * 設置監聽器 */ private void initListener() { mBtnLogin.setOnClickListener(this); } /* 單擊事件監聽 */ @Override public void onClick(View v) { if(v==mBtnLogin){ login(); } } /* 登錄 */ private void login() { final String username = mEtUsername.getText().toString().trim(); final String password = mEtPwd.getText().toString().trim(); if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){ Toast.makeText(MainActivity.this, "用戶名或者密碼不能為空", Toast.LENGTH_SHORT).show(); return; } new Thread(){ @Override public void run() { HttpUtils httpUtils = new HttpUtils(); //轉換為JSON String user = httpUtils.bolwingJson(username, password); //String user ="{'username':" + username + ","+"'password':"+password+"}"; Log.d(TAG, "user:" + user); try { final String result = httpUtils.login(url, user); Log.d(TAG, "結果:" + result); //更新UI,在UI線程中 runOnUiThread(new Runnable() { @Override public void run() { if("SUCCESS".equals(result)){ mTvResult.setText("登錄成功"); }else{ mTvResult.setText("登錄失敗"); } } }); } catch (IOException e) { e.printStackTrace(); } } }.start(); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
這篇文章裡面寫了用seralizable使對象序列化在Activity直之間進行傳遞seralizable是在java api中的類,而在android中也有一個類使對象
確定取消對話框(帶圖標) //(上下文,主題) new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE
前言對於內存洩漏,我想大家在開發中肯定都遇到過,只不過內存洩漏對我們來說並不是可見的,因為它是在堆中活動,而要想檢測程序中是否有內存洩漏的產生,通常我們可以借助LeakC
手機連接不上刷機精靈,有時候不知道怎麼了手機想刷機,可就是連不上刷機精靈,不知道怎麼辦。是手機壞USB壞了嗎?現在小編來幫大家解答一下。怎麼檢查這手機連接不