文章目录
📢题目
🚑使用HttpURLConnection组件或者OkHttp组件模拟登录网站,并读取其中一些数据展示出来
📢效果图
🐱🚀一、开发主页面MainActivity
🎯1.编写主页布局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:background="@drawable/img"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dip"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textColor="#30ED06"
android:text="HTTP客户端登录应用案例"
android:textSize="26sp" />
</LinearLayout>
<TextView
android:id="@+id/c103textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:textSize="18sp"
android:text="演示账号:18772737825,密码:123456"
android:textColor="#fff"
android:textStyle="italic" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="556dp"
android:layout_margin="15px">
<EditText
android:id="@+id/c103userName"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_marginTop="40px"
android:background="#FFFFFF"
android:hint="请输入账号"
android:textSize="20sp">
<requestFocus />
</EditText>
<EditText
android:id="@+id/c103pwd"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_below="@id/c103userName"
android:layout_marginTop="20px"
android:background="#FFFFFF"
android:hint="请输入密码"
android:password="true"
android:textSize="20sp" />
<Button
android:id="@+id/c103okBtn"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_below="@id/c103pwd"
android:layout_alignParentEnd="true"
android:layout_marginTop="27dp"
android:layout_marginEnd="0dp"
android:background="#05F48C"
android:focusable="true"
android:text="登 录"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/c103tipTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/c103okBtn"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dip"
android:text=""
android:textColor="#fff"
android:textStyle="italic" />
</RelativeLayout>
</LinearLayout>
🎯2.编写主页MainActivity.java
package com.example.week10homework;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button okBtn;
EditText userNameET,pwdET;
TextView tipTV;
//食乐网登录地址
String path = "http://www.sl777.cc/API/DataM/SQLData/userLogin";
Handler handler = new Handler(){
public void handleMessage(Message msg){
if(msg.what==1){
String userInfo = String.valueOf(msg.obj);
JSONObject jo = (JSONObject) JSON.parse(userInfo);
String joData = jo.getString("data");
if("".equals(joData) ||"[]".equals(joData)){
tipTV.setText("用户名或密码错误,登陆失败!");
}else {
Intent intent = new Intent(MainActivity.this, LoginOkActivity.class);
intent.putExtra("userInfo", userInfo);
startActivity(intent);
}
}else if(msg.what==0){
tipTV.setText(String.valueOf(msg.obj));
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userNameET = (EditText)findViewById(R.id.c103userName);
pwdET = (EditText)findViewById(R.id.c103pwd);
okBtn= (Button)findViewById(R.id.c103okBtn);
tipTV =(TextView)findViewById(R.id.c103tipTV);
okBtn.getBackground().setAlpha(100);
okBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
final String userName = userNameET.getText().toString();
final String pwd = pwdET.getText().toString();
userNameET.setText("");
pwdET.setText("");
tipTV.setText("");
if(userName.equals("")||pwd.equals("")){
Toast.makeText(MainActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
return ;
}
Runnable runnable =new Runnable(){
public void run(){
try{
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");//设置请求方式为POST
connection.setDoOutput(true);//允许写出
connection.setDoInput(true);//允许读入
connection.setUseCaches(false);//不使用缓存
connection.connect();
String body = "Param={'where':'account="+userName+" and password="+pwd+"'}";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
writer.write(body);
writer.close();
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = connection.getInputStream();
String entityStr = is2String(inputStream);//将流转换为字符串
Message msg = Message.obtain(handler, 1, entityStr);
msg.sendToTarget();
}else{
Message msg = Message.obtain(handler, 0, "网络连接异常");
msg.sendToTarget();
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
new Thread(runnable).start();
}
});
}
public String is2String(InputStream is) throws IOException{
//连接后,创建一个输入流来读取response
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
//每次读取一行,若非空则添加至 stringBuilder
while((line = bufferedReader.readLine()) != null){
stringBuilder.append(line);
}
//读取所有的数据后,赋值给 response;
String result = stringBuilder.toString().trim();
return result;
}
}
🐱🚀二、开发LoginOkActivity页面
🎯1.编写activity_login_ok.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:background="@drawable/img"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dip"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textColor="#30ED06"
android:text="食乐网登录成功"
android:textSize="26sp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:textSize="22sp"
android:text="用户信息如下:"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="@+id/c103userInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:textSize="22sp"
android:textColor="#fff"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50px"
android:textSize="18sp"
android:text="获取JSON数据如下:"
android:textColor="#fff"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/c103srcInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:textSize="18sp"
android:textColor="#fff"
android:textStyle="italic"
/>
</LinearLayout>
🎯2.编写LoginOkActivity.java
package com.example.week10homework;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.week10homework.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class LoginOkActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_ok);
TextView userInfoTV = (TextView)findViewById(R.id.c103userInfo);
TextView srcInfoTV = (TextView)findViewById(R.id.c103srcInfo);
Intent intent=this.getIntent();
Bundle bundle = intent.getExtras();
String userInfo = bundle.getString("userInfo");
JSONObject jo = (JSONObject) JSON.parse(userInfo);
JSONObject userJo =(JSONObject)jo.getJSONArray("data").get(0);
StringBuffer sb = new StringBuffer();
sb.append("账号:").append(userJo.getString("account")).append("\n");
sb.append("密码:").append(userJo.getString("password")).append("\n");
sb.append("电话:").append(userJo.getString("telephone")).append("\n");
sb.append("昵称:").append(userJo.getString("nickName")).append("\n");
sb.append("创建时间:").append(userJo.getString("createTime"));
userInfoTV.setText(sb.toString());
srcInfoTV.setText(userInfo);
}
}