客户端
这是activity
package com.example.mylianjietest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Text2 extends AppCompatActivity {
private EditText eitd_name,edit_id;
private TextView textView,textView1;
public String strng;
private String mBaseUrl="http://10.128.22.164:8080/TextService/";
List<DBShuJu> shuJus;
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what)
{
case 1:
textView1.setText(msg.obj.toString());
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text2);
textView=findViewById(R.id.textView);
edit_id=findViewById(R.id.edit_id);
eitd_name=findViewById(R.id.edit_name);
textView1=findViewById(R.id.textView2);
//获取数据
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//线程
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
try {
URL url=new URL("http://10.128.22.164:8080/TextService/Servlet.servlet");
HttpURLConnection conn= (HttpURLConnection) url
.openConnection();//连接
conn.setRequestMethod("GET");//连接方式
//得到的是输入流 转换成json字符串
InputStream in=conn.getInputStream();
String resuli=StreamUtils.readStream(in);//获取里面的数据
//解析json字符串
JSONObject jsonObject=new JSONObject(resuli);
String recruits=jsonObject.getString("recuitString");
// 用GSON解析集合
Gson gson=new Gson();
shuJus=gson.fromJson(recruits,new TypeToken<List<DBShuJu>>(){}.getType());
DBShuJu dbShuJu=shuJus.get(1);
Toast.makeText(Text2.this, "接收数据"+dbShuJu.getNameString(), Toast.LENGTH_SHORT).show();
Message message=new Message();
message.obj=dbShuJu.nameString;
message.what=1;
handler.sendMessage(message);
} catch (IOException | JSONException e) {
e.printStackTrace();
}
Looper.loop();
}
}).start();
}
});
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doGet(v);
}
});
}
//传送数据
public void doGet(View view){
OkHttpClient okHttpClient=new OkHttpClient();
Request.Builder builder=new Request.Builder();
//传输数据
Request request=builder.get().url(mBaseUrl+"login?username="+eitd_name.getText().toString()+"&password="+edit_id.getText().toString()).build();
Call call=okHttpClient.newCall(request);
//执行get请求 异步执行
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//发生错误时回调
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//正确执行
strng=response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(strng);
}
});
}
});
}
}
class DBShuJu.java
package com.example.mylianjietest;
public class DBShuJu {
int id;
String nameString;
String beizhuString;
//用于URL传参和取参时的key
public static String ID = "id";
public static String NAME = "name";
//构造方法 Source->G C u f
// public DBShuJu(int id, String nameString, String beizhuString) {
// super();
// this.id = id;
// this.nameString = nameString;
// this.beizhuString = beizhuString;
// }
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNameString() {
return nameString;
}
public void setNameString(String nameString) {
this.nameString = nameString;
}
public String getBeizhuString() {
return beizhuString;
}
public void setBeizhuString(String beizhuString) {
this.beizhuString = beizhuString;
}
public void setId(String string) {
// TODO Auto-generated method stub
}
}