如何使用HttpURLConnection仅发送一个请求

时间:2019-03-27 02:06:56

标签: java android get httpurlconnection java-threads

我是编码方面的新手,这是我第一次发布问题。

  1. 我正在使用以下代码向服务器发出请求。
  2. 我一次调用了该函数,但是请求被发送了两次。
  3. 我从服务器获得的结果来自第二个请求结果。

如何仅向服务器发送一个请求

我存储所有可运行对象的类

public class ServerRunnables implements Runnable {
    Account currentAccount = Account.getInstance();

    private String result = "";
    private Context context;
    private Boolean callSuccess = true;

    int serviceCalled;
    String email;
    String password;
    String username;
    int coinChanges;

    String urlString = "http://10.0.2.2/MobileCoin/" + services[serviceCalled];
    String requiredParams;

    public ServerRunnables(Context context,
                           int serviceCalled,
                           String email,
                           String password,
                           String username,
                           int coinChanges) {

        this.context = context;
        this.serviceCalled = serviceCalled;
        this.email = email;
        this.password = password;
        this.username = username;
        this.coinChanges = coinChanges;
    }

    public void run() {
        urlString = "http://10.0.2.2/MobileCoin/" + services[serviceCalled];
        switch (serviceCalled) {
            case SERVICE_RANDOM_COIN:
                generateCoinServiceCalled();
                break;
            default:
                break;
        }
    }



    private void generateCoinServiceCalled() {

        requiredParams = "email=" + email;
        urlString = urlString + "?" + requiredParams;

        getMethodCalled(urlString);

        callSuccess = true;
        String message1 = "";

        if (result.equals(ServerConstants.RANDOM_COIN_EMPTY)) {
            callSuccess = false;
            message1 = "No Data Received";
        } else if (result.equals(ServerConstants.RANDOM_COIN_FAIL)) {
            callSuccess = false;
            message1 = "Data Does not Exist.";
        } else if (result.equals(ServerConstants.CONNECTION_ERROR)) {
            callSuccess = false;
            message1 = "Connection Error, Check Server";
        } else {
            message1 = "Coin Generated : " + Integer.parseInt(result);
        }

        coinChanges(currentAccount.getCoin() + Integer.parseInt(result));
    }

    private void getMethodCalled(String urlString) {
        result = "";

        try {
            URL url = new URL(urlString);

            HttpURLConnection hc = (HttpURLConnection) url.openConnection();
            int length = hc.getContentLength();

            InputStream input = url.openStream();
            byte[] binput = new byte[length];
            int readSize = input.read(binput);

            result = new String(binput).trim();
            input.close();
        } catch (Exception e) {
            Log.e("Net", "Error", e);
            result = ServerConstants.CONNECTION_ERROR;
        }

        Log.i(TAG, result);
    }
}

从此处开始发送请求

public class ServletsAPI {
    public static Context context;

    public static void randomCoinAPI(String email){
        ServerRunnables newRunnable =
                new ServerRunnables(context, ServerRunnables.SERVICE_RANDOM_COIN, email, null, null, 0);
        new Thread(newRunnable).start();
    }
}

2 个答案:

答案 0 :(得分:0)

为什么不使用Retrofit轻松处理http请求?如果与RxJava集成,则可以异步管理请求。

答案 1 :(得分:0)

我发现了这个错误:

InputStream input = url.openStream();

更改为

InputStream input = hc.getInputStream();