我还是Android编程的新手,尽管我了解很多,但这个错误使我困扰了很长时间,尽管我很了解AsyncTask()概念。请提供您宝贵的建议。 我已经制作了一个天气应用程序,它使用jsoup和一些Web抓取功能,曾经可以完美运行,现在却出现此错误,不知道该怎么办。非常感谢您的帮助。
代码如下:
pip freeze
这是错误报告:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.AsyncTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
static TextView weatherInfo;
TextView x_view;
TextView y_view;
EditText x_edit;
EditText y_edit;
Button submit_button;
String cord_x;
String cord_y;
String f_coord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
weatherInfo = findViewById(R.id.weather_info);
x_view = findViewById(R.id.X_View);
y_view = findViewById(R.id.Y_view);
x_edit = findViewById(R.id.x_edittext);
y_edit = findViewById(R.id.y_edittext);
submit_button = findViewById(R.id.submit_button);
submit_button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
weatherInfo.setText("");
cord_x=x_edit.getText().toString();
cord_y=y_edit.getText().toString();
f_coord=cord_x+","+cord_y;
Weather runner = new Weather();
runner.execute(f_coord);
}
}
);
}
static class Weather extends AsyncTask<String, String, String> {
String result = null;
@Override
protected String doInBackground(String... string) {
try {
Document doc = Jsoup.connect("https://weather.com/en-IN/weather/today/l/"+string[0]).get();
for (Element row : doc.select("header[class=loc-container]")) {
result=string[0]+" \nLocation: ";
result = result+row.text()+" ";
}
for (Element row : doc.select("div[class=today_nowcard-section today_nowcard-condition]")) {
result = result +"\nWeather Data:\n"+ (row.text());
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String s) {
weatherInfo.append(s);
}
}
}
答案 0 :(得分:3)
您只是忘记在INTERNET
文件中添加 manifest
权限
在清单文件中添加以下权限即可使用
<uses-permission android:name="android.permission.INTERNET" />
答案 1 :(得分:1)
像这样在清单文件中添加Internet权限
<manifest xlmns:android...>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
答案 2 :(得分:1)
您在AndroidManifest文件中缺少INTERNET权限。请在AndroidManifest中添加以下权限以解决此问题:
<uses-permission android:name="android.permission.INTERNET" />
答案 3 :(得分:1)
遍历堆栈跟踪指示清单文件中需要哪种权限。
Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
at java.net.InetAddress.lookupHostByName(InetAddress.java:464)
Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
请按如下所示添加权限,它将进行整理
<uses-permission android:name="android.permission.INTERNET" />