Android使用OkHttp發(fā)送post請求
本文實例為大家分享了使用OkHttp發(fā)送post請求的具體代碼,供大家參考,具體內(nèi)容如下
MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText mEt_qq;
private EditText mEt_pwd;
private TextView mTv_status;
String path = "http://169.254.53.96:8080/web/LoginServlet";
private static final int SUCCESS = 665;
private static final int FALL = 894;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SUCCESS:
String text= (String) msg.obj;
mTv_status.setText(text);
break;
case FALL:
Toast.makeText(MainActivity.this, "沒有網(wǎng)", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//對控件進(jìn)行初始化操作
initVIew();
}
private void initVIew() {
mEt_qq = (EditText) findViewById(R.id.et_qq);
mEt_pwd = (EditText) findViewById(R.id.et_pwd);
mTv_status = (TextView) findViewById(R.id.tv_status);
}
/**
* 使用Post進(jìn)行表單(鍵值對)上傳,完成登錄
* @param view
*/
public void login(View view){
//得到用戶輸入的信息,進(jìn)行非空判斷
String qq = mEt_qq.getText().toString().trim();
String pwd =mEt_pwd.getText().toString().trim();
if(TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd) ){
Toast.makeText(MainActivity.this, "不能輸入為空", Toast.LENGTH_SHORT).show();
return;
}
//1.0 創(chuàng)建okhttpClinet
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10,TimeUnit.SECONDS)
.writeTimeout(10,TimeUnit.SECONDS)
.build();
FormBody formBody= new FormBody.Builder()
.add("qq", qq).add("pwd", pwd)
.build();
Request request= new Request.Builder()
.post(formBody)
.url(path)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
handler.sendEmptyMessage(FALL);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Message msg = Message.obtain();
msg.obj=string;
msg.what=SUCCESS;
handler.sendMessage(msg);
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et_qq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入qq號碼" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼"
android:inputType="textPassword" />
<Button
android:onClick="login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陸" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_status"
android:text="登陸狀態(tài):"
/>
</LinearLayout>
build.gradle //依賴
implementation 'com.squareup.okhttp3:okhttp:3.4.2'
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- OkHttp攔截器在Android網(wǎng)絡(luò)中的使用和工作原理
- Android入門之使用OKHttp多線程下載文件
- Android 使用 okhttp3和retrofit2 進(jìn)行單文件和多文件上傳
- Android基于OkHttp實現(xiàn)文件上傳功能
- Android使用OKhttp3實現(xiàn)登錄注冊功能+springboot搭建后端的詳細(xì)過程
- Android的簡單前后端交互(okHttp+springboot+mysql)
- Android Okhttp斷點續(xù)傳面試深入解析
- Android使用OkHttp進(jìn)行網(wǎng)絡(luò)同步異步操作
- Android視頻/音頻緩存框架AndroidVideoCache(Okhttp)詳解
- Android OkHttp實現(xiàn)全局過期token自動刷新示例
- OkHttp原理分析小結(jié)
相關(guān)文章
Android 關(guān)閉多個Activity的實現(xiàn)方法
這篇文章主要介紹了Android 關(guān)閉多個Activity的實現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
Android中加載網(wǎng)絡(luò)資源時的優(yōu)化可使用(線程+緩存)解決
Android 中加載網(wǎng)絡(luò)資源時的優(yōu)化;基本的思路是線程+緩存來解決,具體解決思路如下,有類似情況的朋友可以參考下哈2013-06-06
Android中Fragment子類及其PreferenceFragment的創(chuàng)建過程演示
這篇文章主要介紹了Android中Fragment子類及其PreferenceFragment的創(chuàng)建過程演示,PreferenceFragment用來保存Fragment的選項設(shè)置,需要的朋友可以參考下2016-05-05
Android dataBinding與ListView及事件詳解
這篇文章主要介紹了Android dataBinding與ListView及事件詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android常用控件ImageSwitcher使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android常用控件ImageSwitcher的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
android中圖片的三級緩存cache策略(內(nèi)存/文件/網(wǎng)絡(luò))
實現(xiàn)圖片緩存也不難,需要有相應(yīng)的cache策略。這里我采用 內(nèi)存-文件-網(wǎng)絡(luò) 三層cache機制,其中內(nèi)存緩存包括強引用緩存和軟引用緩存(SoftReference),其實網(wǎng)絡(luò)不算cache,這里姑且也把它劃到緩存的層次結(jié)構(gòu)中2013-06-06

