How can I make a simple HTTP request in MainActivity.java? (Android Studio)

后端 未结 3 1940
無奈伤痛
無奈伤痛 2020-12-30 15:03

I\'m using Android Studio, and I\'ve spent a few hours trying to do a simple HTTP request in my MainActivity.java file, and tried multiple ways, and seen many web pages on t

3条回答
  •  感情败类
    2020-12-30 15:56

    You can use dependency for making HTTP requests or HTTPS request,Use OkHttp

    Visit :https://square.github.io/okhttp

     OkHttpClient client = new OkHttpClient();
    
    String run(String url) throws IOException {
      Request request = new Request.Builder()
          .url(url)
          .build();
    
      try (Response response = client.newCall(request).execute()) {
        return response.body().string();
      }
    }
    

提交回复
热议问题