安卓okhttp3进行网络请求,一个简单的登录页面的实现

文章目录

一、前言

 写在文章的前面:为了不浪费大家的时间,本文只是为了记录一下自己开发过程中遇到的问题,而且这只是一个最简单的登录页面的实
 现,不能解决安卓大佬开发上的任何问题,只是给我自己或者和我一样的0基础的人看的。

 本文通过okhttp3实现安卓app进行后台webapi的网络访问。
 初学android,想开发一个简单的app,在网上查阅了大量的资料,但是都是一些很老的教程了,虽然适用,但是讲的不是很清楚,对
 没有系统学习过安卓的人来说很不友好。

二、配置okhttp3

<!--在app目录下的 build.gradle文件中添加 -->

dependencies {
   
    implementation("com.squareup.okhttp3:okhttp:4.1.0")
}

<!-- AndroidManifest.xml文件中添加 -->
<!--开启网络权限,不然不能进行网络访问-->
<uses-permission android:name="android.permission.INTERNET" />

三、activity_main.xml文件

 因为是登录界面,并且只作为示范,所以样式简陋,将就着能用就好。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edit_username"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="160dp"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="116dp"
        android:layout_marginEnd="38dp"
        android:layout_marginRight="38dp"
        android:ems="10"
        android:hint="@string/edit_message"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.688"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width=
  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值