Androidの単純な前後相互作用(ok Http+springboot+mysql)
前言
この間、面白くて使いやすいフレームを見つけました。カリキュラムの設計が必要なので、なんとなくこのフレームを発見しました。このフレームを利用してバックエンドとインタラクションしたいです。前後の端の分離項目を参考にして、androidをフロントエンドとして、springbootをバックエンドとして、以下は両者の簡単なインタラクションです。
okHttp説明
(1)androidネットワークフレームのOKhttp
ネットワーク要求を処理するオープンソースプロジェクトは、安卓端の最も熱いライト級のフレームワークであり、モバイル決済スクウェア会社によって貢献された(同社はPicassoにも貢献した)。
HttpUrlConnectionとApphe Http Clientの代わりに使用します。
(2)ok Http優勢
同じホストアドレスに接続するすべての要求を許可し、要求効率を向上させる。
ソケットを共有し、サーバに対する要求回数を減らす。
接続池を通じて要求の遅延が少なくなりました。
キャッシュ応答データは、重複したネットワーク要求を低減する。
データ流量の消費が減少しました。
自動処理GZip圧縮
(3)OKhttpの機能
get、postリクエスト
ファイルのアップロード
画像を読み込む(内部は画像サイズが自動的に圧縮されます)
要求のコールバックをサポートして、直接にオブジェクト、オブジェクトのセットに戻ります。
セッションの保持をサポートします。
android先端
論理制御:LoginaActivity.java
(1)データベース
テーブルuser
(2)スプリングブックにおけるcontroller層
どのようにspringbootプロジェクトを構築するかはもう詳しく説明しません。(必要があれば、メッセージを残してもいいです。後で教程を作ることができます。)他の文章を参考にしてください。
(1)「登録」ボタンをクリックし、ポスト要求を開始する
android端
バックエンド
(2)「登録」ボタンをクリックしてget要求を開始する
android端
バックエンド
これで前後端分離の効果が得られます。不思議ではないですか?グループメンバーと分けて楽しく開発できます。
Android端末では、個人的な組み合わせで作成するok Httpのツール類を使用しています。上記の記事を参照してください。okHttpのgetとpost要求の簡単なパッケージと使用
ここで、Androidに関する簡単な前後相互作用についての文章を紹介します。これに関連して、Androidの前後の相互作用がもっと多いので、私達の以前の文章を検索してください。または次の関連記事を引き続きご覧ください。これからもよろしくお願いします。
この間、面白くて使いやすいフレームを見つけました。カリキュラムの設計が必要なので、なんとなくこのフレームを発見しました。このフレームを利用してバックエンドとインタラクションしたいです。前後の端の分離項目を参考にして、androidをフロントエンドとして、springbootをバックエンドとして、以下は両者の簡単なインタラクションです。
okHttp説明
(1)androidネットワークフレームのOKhttp
ネットワーク要求を処理するオープンソースプロジェクトは、安卓端の最も熱いライト級のフレームワークであり、モバイル決済スクウェア会社によって貢献された(同社はPicassoにも貢献した)。
HttpUrlConnectionとApphe Http Clientの代わりに使用します。
(2)ok Http優勢
同じホストアドレスに接続するすべての要求を許可し、要求効率を向上させる。
ソケットを共有し、サーバに対する要求回数を減らす。
接続池を通じて要求の遅延が少なくなりました。
キャッシュ応答データは、重複したネットワーク要求を低減する。
データ流量の消費が減少しました。
自動処理GZip圧縮
(3)OKhttpの機能
get、postリクエスト
ファイルのアップロード
画像を読み込む(内部は画像サイズが自動的に圧縮されます)
要求のコールバックをサポートして、直接にオブジェクト、オブジェクトのセットに戻ります。
セッションの保持をサポートします。
android先端
論理制御:LoginaActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.campus.book.R;
import com.campus.book.entity.User;
import com.campus.book.util.http.OKHttpUtil;
import com.google.gson.Gson;
public class LoginActivity extends AppCompatActivity {
// url cmd ipconfig IPv4 8081
private String baseUrl="http://192.168.xxx.1:8081";
private TextView tv=null;
EditText userId = null;
EditText pwd = null ;
Button login=null;
private Button registry=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
setTitle(" ");
tv=findViewById(R.id.tv);
login = (Button)findViewById(R.id.login);
registry = (Button)findViewById(R.id.registry);
userId=(EditText) findViewById(R.id.userId);
pwd=findViewById(R.id.pwd);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id = userId.getText().toString();
String password=pwd.getText().toString();
User user=new User(id,password);
Gson gson=new Gson();
String json=gson.toJson(user);
String args[]=new String[]{"user","login"};
String res= OKHttpUtil.postSyncRequest(baseUrl,json,args);
Log.d(" :",res);
res= OKHttpUtil.postAsyncRequest(baseUrl,json,args);
Log.d(" :",res);
}
});
registry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String args[]=new String[]{"user","getUser","123"};
String res= OKHttpUtil.getSyncRequest(baseUrl,args);
System.out.println(" :"+res);
String args1[]=new String[]{"user","getUser","123"};
res= OKHttpUtil.getAsyncRequest(baseUrl,args1);
System.out.println(" :"+res);
}
});
}
}
レイアウト方式:activitylogin.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.LoginActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:text=" :"/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/mainBg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/login" />
<!--@drawable/login -->
<TableLayout
android:layout_width="350dp"
android:layout_height="match_parent"
android:stretchColumns="*"
android:layout_marginBottom="150sp"
android:layout_gravity="center" >
<TableRow android:layout_height="match_parent">
<EditText
android:id="@+id/userId"
android:layout_column="0"
android:layout_span="2"
android:hint=" "
android:textColorHint="#FFFFFF"
android:textColor="#FFFFFF"
android:textCursorDrawable="@drawable/cursor_color"
android:textSize="15sp" />
</TableRow>
<TableRow android:layout_height="match_parent" >
<EditText
android:id="@+id/pwd"
android:inputType="textPassword"
android:layout_column="0"
android:layout_span="2"
android:hint=" "
android:textColorHint="#FFFFFF"
android:textColor="#FFFFFF"
android:textCursorDrawable="@drawable/cursor_color"
android:textSize="15sp" />
</TableRow>
<TableRow android:layout_height="match_parent">
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:background="#000000"
android:layout_margin="8dp"
android:textSize="15sp"
android:text=" " />
<Button
android:id="@+id/registry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:background="#000000"
android:layout_margin="8dp"
android:textSize="15sp"
android:text=" " />
</TableRow>
</TableLayout>
</FrameLayout>
</LinearLayout>
その中で、cursor_color.xmlはdrawableの中にあります。
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<size android:width="2dp" />
<solid android:color="@android:color/black" />
</shape>
springboot+mysql後端(1)データベース
テーブルuser
(2)スプリングブックにおけるcontroller層
どのようにspringbootプロジェクトを構築するかはもう詳しく説明しません。(必要があれば、メッセージを残してもいいです。後で教程を作ることができます。)他の文章を参考にしてください。
@RestController
@RequestMapping("//user")
public class UserController {
@Autowired
private UserService userService;
//Gson gson= JsonBean.getGson();
static Gson gson=new GsonBuilder().serializeNulls().create();
@GetMapping("/list")
public List<User> list() {
return this.userService.list();
}
@PostMapping("/login")
public User login(String json){
User result=null;
User user=null;
User user1=null;
try{
user=gson.fromJson(json,User.class);
}catch (Exception e){
e.printStackTrace();
}
user1=userService.getById(user.getUserId());
if(user1!=null){//
if(user1.getPassword().equals(user.getPassword())){//
result=user1;
}else{//
}
}else{//
}
return result;
}
@GetMapping("/getUser/{id}")
public User getUser(@PathVariable("id") Serializable id){
User user=userService.getById(id);
if(user!=null){//
}else{//
}
return user;
}
}
実行(インタラクティブ)効果(1)「登録」ボタンをクリックし、ポスト要求を開始する
android端
バックエンド
(2)「登録」ボタンをクリックしてget要求を開始する
android端
バックエンド
これで前後端分離の効果が得られます。不思議ではないですか?グループメンバーと分けて楽しく開発できます。
Android端末では、個人的な組み合わせで作成するok Httpのツール類を使用しています。上記の記事を参照してください。okHttpのgetとpost要求の簡単なパッケージと使用
ここで、Androidに関する簡単な前後相互作用についての文章を紹介します。これに関連して、Androidの前後の相互作用がもっと多いので、私達の以前の文章を検索してください。または次の関連記事を引き続きご覧ください。これからもよろしくお願いします。