安卓访问自己的servlet失败用户名密码地址都一样的Postman访问成功,来个大佬啊这要这么解决

我用的雷电模拟器下面是我的

OkhttpService
package com.example.myapplication.service;

import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkhttpService {
    private Handler handler;

    public OkhttpService(Handler handler) {
        this.handler = handler;
    }

    public void loginByOKHttpAsynPost(String name, String pwd) {
        OkHttpClient client = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.SECONDS).build();
        String path = "http://10.154.135.142:8080/Android/AndroidLogin";
        RequestBody body = new FormBody.Builder().add("id", name).add("password", pwd).build();
        Request request = new Request.Builder().url(path).post(body).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Message message = new Message();
                message.obj = "访问失败";
                handler.sendMessage(message);
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Message message = new Message();
                message.obj = response.body().string();
                handler.sendMessage(message);
            }
        });
    }


}
okhttpService.loginByOKHttpAsynPost(name,pwd);
Handler handler=new Handler(Looper.getMainLooper()){
    @Override
    public void handleMessage(@NonNull Message msg) {
        String result=(String) msg.obj;
        if (!result.equals("登录成功")){
            Toast.makeText(LoginActivity.this, result, Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(LoginActivity.this, result, Toast.LENGTH_SHORT).show();
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值