问题描述
我正在使用Android Studio,我花了几个小时尝试在我的MainActivity.java文件中做一个简单的HTTP请求,并尝试了多种方法,并看到了很多关于这个主题的网页,却无法理解out。
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 the subject, yet cannot figure it out.
当我尝试OkHttp时,我收到一条关于无法在主线程上执行此操作的错误。现在我试着这样做:
When I try OkHttp, I get a error about not being able to do it on the main thread. Now I'm trying to do it this way:
public static String getUrlContent(String sUrl) throws Exception {
URL url = new URL(sUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String content = "", line;
while ((line = rd.readLine()) != null) {
content += line + "\n";
}
return content;
}
我将该方法直接放在MainActivity.java中,我的click事件执行它另一种方法也在MainActivity.java中:
I put that method directly in MainActivity.java, and my click event executes it from another method that is also in MainActivity.java:
try {
String str = getUrlContent("https://example.com/WAN_IP.php");
displayMessage(str);
}
catch(Exception e){
displayMessage(e.getMessage());
}
但是现在没有崩溃,我可以说有一个例外在启动BufferedReader的行上抛出,但是e.getMessage()是空白的。
But right now there is no crash, and I can tell there is an exception thrown on the line that starts "BufferedReader", but e.getMessage() is blank.
我是Android Studio和java的新手,所以请善待帮助我解决这个非常基本的问题。最终我需要向服务器发送请求,看起来OkHttp是最好的方式,但我没有在Android Studio中找到OkHttp的Hello World,这在任何地方都可以在网上找到。
I'm brand new to Android Studio and java, so please be kind and help me with this very basic problem. Eventually I will need to do post requests to the server, and it seems that OkHttp is the best way to go, but I'm not finding the "Hello World" of OkHttp in Android Studio documented anywhere online.
推荐答案
您不应在主线程上发出网络请求。延迟是不可预测的,它可能会冻结用户界面。
You should not make network requests on the main thread. The delay is unpredictable and it could freeze the UI.
如果您使用 HttpUrlConnection $ c $,Android会通过抛出异常来强制执行此操作c>来自主线程的对象。
Android force this behaviour by throwing an exception if you use the HttpUrlConnection
object from the main thread.
然后,您应该在后台发出网络请求,然后更新主线程上的UI。 对于这个用例,class非常方便!
You should then make your network request in the background, and then update the UI on the main thread. The AsyncTask
class can be very handy for this use case !
private class GetUrlContentTask extends AsyncTask<String, Integer, String> {
protected String doInBackground(String... urls) {
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String content = "", line;
while ((line = rd.readLine()) != null) {
content += line + "\n";
}
return content;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(String result) {
// this is executed on the main thread after the process is over
// update your UI here
displayMessage(result);
}
}
你以这种方式开始这个过程:
And you start this process this way:
new GetUrlContentTask().execute(sUrl)
这篇关于如何在MainActivity.java中创建简单的HTTP请求? (Android Studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!