qq-jsp页面登录qq
程序员文章站
2022-07-05 09:17:53
// An highlighted blockpackage com.example.qq_login_fuwuqi;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Messag...
// An highlighted block
package com.example.qq_login_fuwuqi;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class MainActivity extends AppCompatActivity {
EditText editText_name,editText_password;
ImageButton imageButton;
String result;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText_name=findViewById(R.id.edit_1);
editText_password=findViewById(R.id.edit_2);
imageButton=findViewById(R.id.imgBtn);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result="";
if(editText_name.getText().toString().equals("")||
editText_password.getText().toString().equals(""))
{
Toast.makeText(MainActivity.this,"请输入用户名或密码",Toast.LENGTH_SHORT).show();
}
else
{
handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
if("ok".equals(result))
{
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}
else
{
Toast.makeText(MainActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
}
super.handleMessage(msg);
}
};
new Thread(new Runnable() {
@Override
public void run() {
Send();
Message message=handler.obtainMessage();
handler.sendMessage(message);
}
}).start();
}
}
});
}
public void Send()
{
String address="http://10.211.248.235:8080/android/post.jsp";
try {
URL url=new URL(address);
HttpURLConnection coon= (HttpURLConnection) url.openConnection();
coon.setRequestMethod("POST");//指定使用post请求
coon.setDoInput(true);//写入数据
coon.setDoOutput(true);//读入数据
coon.setUseCaches(false);//禁止使用缓存
coon.setInstanceFollowRedirects(true);//自动执行http重定向
coon.setRequestProperty("Content-type","application/x-www-form-urlencoded");//参数类型设置
DataOutputStream dos=new DataOutputStream(coon.getOutputStream());
String param="username="+ URLEncoder.encode(editText_name.getText().toString(),"utf-8")
+"&password="+URLEncoder.encode(editText_password.getText().toString(),"utf-8");
dos.writeBytes(param);//将数据写入流
dos.flush();
dos.close();
if(coon.getResponseCode()==HttpURLConnection.HTTP_OK);
{
InputStreamReader isr=new InputStreamReader(coon.getInputStream());
BufferedReader br=new BufferedReader(isr);
String data="";
while((data=br.readLine())!=null)
{
result+=data;
}
isr.close();
}
coon.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
本文地址:https://blog.csdn.net/sumaiLme/article/details/112260505