标签:
android开发中网络通讯必不可少,目前使用率较高的http框架有Okhttp、nohttp、volley等等,
下面做一个用户登陆的demo,说明一下Okhttp的用法,废话不多说,看代码。
LoginActivity.java
1 package com.junyi.shangqifixture; 2 3 import java.io.IOException; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.os.AsyncTask; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.EditText; 13 import okhttp3.FormBody; 14 import okhttp3.OkHttpClient; 15 import okhttp3.Request; 16 import okhttp3.RequestBody; 17 import okhttp3.Response; 18 19 public class LoginActivity extends Activity { 20 21 private final OkHttpClient client = new OkHttpClient(); 22 private EditText editAccount,editPwd; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 // TODO Auto-generated method stub 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_login); 29 30 editAccount=(EditText)findViewById(R.id.accountEdittext); 31 editPwd=(EditText)findViewById(R.id.pwdEdittext); 32 33 Button btnLogin=(Button)findViewById(R.id.login_in); 34 btnLogin.setOnClickListener(new OnClickListener() { 35 36 @Override 37 public void onClick(View arg0) { 38 // TODO Auto-generated method stub 39 new AnsyTry().execute(editAccount.getText().toString(),editPwd.getText().toString()); 40 } 41 }); 42 } 43 44 private class AnsyTry extends AsyncTask<String, Integer, String>{ 45 46 @Override 47 protected String doInBackground(String... params) { 48 // TODO Auto-generated method stub 49 try { 50 RequestBody formBody = new FormBody.Builder() 51 .add("account", params[0]) 52 .add("pwd", params[1]) 53 .build(); 54 55 Request request = new Request.Builder() 56 .url("http://baidu.com") 57 .post(formBody) 58 .build(); 59 60 Response response = client.newCall(request).execute(); 61 if(response.isSuccessful()){ 62 return response.body().string(); 63 } 64 } catch (IOException e) { 65 // TODO Auto-generated catch block 66 e.printStackTrace(); 67 } 68 return null; 69 } 70 71 @Override 72 protected void onPostExecute(String result) { 73 // TODO Auto-generated method stub 74 super.onPostExecute(result); 75 76 //TODO 此处判断返回值 77 78 Intent intent = new Intent(); 79 intent.setClass(LoginActivity.this, MainActivity.class); 80 startActivity(intent); 81 finish(); 82 } 83 } 84 }
activity_login.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:background="@drawable/bkcolor" 6 android:orientation="vertical" > 7 8 <ImageView 9 android:id="@+id/iv1" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:layout_gravity="center" 13 android:layout_marginTop="20dp" 14 android:src="@drawable/head" /> 15 16 <RelativeLayout 17 android:id="@+id/editRel" 18 android:layout_width="fill_parent" 19 android:layout_height="wrap_content" 20 android:layout_marginTop="20dp" > 21 22 <RelativeLayout 23 android:id="@+id/accountRel" 24 android:layout_width="fill_parent" 25 android:layout_height="wrap_content" 26 android:layout_marginTop="14dp" 27 android:background="@drawable/preference_first_item" 28 android:clickable="true" 29 android:gravity="center_vertical" > 30 31 <TextView 32 android:id="@+id/account" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:padding="8dp" 36 android:text="@string/account" 37 android:textColor="#000" 38 android:textSize="17sp" /> 39 40 <EditText 41 android:id="@+id/accountEdittext" 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:layout_centerVertical="true" 45 android:layout_marginLeft="20dip" 46 android:layout_toRightOf="@id/account" 47 android:background="@null" 48 android:hint="@string/account" 49 android:text="test" 50 android:textSize="15sp" /> 51 </RelativeLayout> 52 53 <RelativeLayout 54 android:id="@+id/pwdRel" 55 android:layout_width="fill_parent" 56 android:layout_height="wrap_content" 57 android:layout_below="@id/accountRel" 58 android:background="@drawable/preference_last_item" 59 android:clickable="true" 60 android:gravity="center_vertical" > 61 62 <TextView 63 android:id="@+id/pwd" 64 android:layout_width="wrap_content" 65 android:layout_height="wrap_content" 66 android:padding="8dp" 67 android:text="@string/pwd" 68 android:textColor="#000" 69 android:textSize="17sp" /> 70 71 <EditText 72 android:id="@+id/pwdEdittext" 73 android:layout_width="fill_parent" 74 android:layout_height="wrap_content" 75 android:layout_centerVertical="true" 76 android:layout_marginLeft="20dip" 77 android:layout_toRightOf="@id/pwd" 78 android:background="@null" 79 android:hint="@string/pwd" 80 android:text="test" 81 android:inputType="textPassword" 82 android:textSize="15sp" /> 83 </RelativeLayout> 84 </RelativeLayout> 85 86 <Button 87 android:id="@+id/login_in" 88 style="@style/common_button_style" 89 android:layout_margin="10dp" 90 android:text="@string/login_in" /> 91 92 <TextView 93 android:id="@+id/sound_help" 94 android:layout_width="wrap_content" 95 android:layout_height="wrap_content" 96 android:layout_marginLeft="15dip" 97 android:layout_marginTop="10dip" 98 android:text="@string/forget_pwd" 99 android:textColor="#29981A" /> 100 101 </LinearLayout>
2016-05-31 14:52:30
标签:
原文地址:http://www.cnblogs.com/jiwenbin010/p/5545861.html