package com.example.myapplication;
import android.nfc.Tag;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private EditText username;
private EditText pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = findViewById(R.id.username);
pwd = findViewById(R.id.password);
}
//Get请求
public void LoginGet(View view){
String name=username.getText().toString();
String password=pwd.getText().toString();
String path="http://172.18.43.147:8080/VATUU_war_exploded/Login?username="+name+"&password="+password;
System.out.println(path);
System.out.println("用户名:"+name);
System.out.println("密码:"+password);
OkHttpClient client =new OkHttpClient();
Request request=new Request.Builder().url(path).get().build();
client.newCall(request).enqueue(new Callback() {
//失败
@Override
public void onFailure(Call call, IOException e) {
Log.d("MainActivity","连接失败"+e.getLocalizedMessage());
}
//成功
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
Log.d("MainActivity",result);
if (response.body()!=null){
response.body().close();
}
}
});
}
//Post请求
public void parseJSONWithJSONObject(String jsonData) {
try {
// 把需要解析的数据传入到 JSONArray 对象中
JSONArray jsonArray = new JSONArray(jsonData);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String id = jsonObject.getString("xh");
Log.d("JSONObject解析", "id is " + id);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void LoginPost(View view){
String name=username.getText().toString();
String password=pwd.getText().toString();
String area="1";
String building ="13";
String week="10";
String xq="5";
//服务器地址String url="http://82.157.106.56:8009/vtServlet";
String url="http://172.18.46.247:8080/VATUU_war_exploded/vtServlet";
System.out.println(url);
System.out.println("用户名:"+name);
System.out.println("密码:"+password);
OkHttpClient client=new OkHttpClient();
//构建表单参数
FormBody.Builder requestBuild=new FormBody.Builder();
//添加请求体
RequestBody requestBody=requestBuild
.add("area",area)
.add("building",building)
.add("week",week)
.add("xq",xq)
.build();
Request request=new Request.Builder()
.url(url)
.post(requestBody)
.build();
System.out.println(request.toString());
//异步请求
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d("MainActivityPost","连接失败"+e.getLocalizedMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
parseJSONWithJSONObject(result);
Log.d("MainActivity",result);
if (response.body()!=null){
response.body().close();
}
}
});
}
}