Android studio Android Studio WhitelistProcess-进程被列入白名单
我刚刚通过Android Studio创建了一个天气预报应用程序。当我运行project时,它没有使用API,并且显示为空。所以我查了一下Logcat的节目 2020-11-03 03:54:14.7901560-1560/?E/合子:isWhitelistProcess-流程被列入白名单 2020-11-03 03:54:14.792150-1560/?E/libpersona:scanKnoxPersonas 2020-11-03 03:54:14.793150-1560/?E/libpersona:无法打开文件-/data/system/users/0/personalist.xml-没有这样的文件或目录 这是我的密码:Android studio Android Studio WhitelistProcess-进程被列入白名单,android-studio,Android Studio,我刚刚通过Android Studio创建了一个天气预报应用程序。当我运行project时,它没有使用API,并且显示为空。所以我查了一下Logcat的节目 2020-11-03 03:54:14.7901560-1560/?E/合子:isWhitelistProcess-流程被列入白名单 2020-11-03 03:54:14.792150-1560/?E/libpersona:scanKnoxPersonas 2020-11-03 03:54:14.793150-1560/?E/libper
package com.example.project_weatherforecast;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpRequest {
public static String excuteGet(String targetURL){
URL url;
HttpURLConnection connection= null;
try {
url= new URL(targetURL);
connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream;
int status= connection.getResponseCode();
if(status != HttpURLConnection.HTTP_OK)
inputStream= connection.getErrorStream();
else
inputStream= connection.getInputStream();
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuffer response= new StringBuffer();
while((line = bufferedReader.readLine())!= null){
response.append(line);
response.append('\r');
}
bufferedReader.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null){
connection.disconnect();
}
}
}
和主要活动:
package com.example.project_weatherforecast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
String API = "API: 5f0e7bb0ea868b77d281b07c046f565c";
String city = "Hanoi";
TextView address, status, recentTemp, tempMax, tempMin, date, description, sunSet, sunRise,
windSpeed, pressure, humidity, feelsLike;
ImageView image, sunSetImg, sunRiseImg, windImg, pressureImg, humidityImg, feelImg;
ImageButton btnSearch;
EditText etSearch;
View mainView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar= getSupportActionBar();
actionBar.hide();
address = findViewById(R.id.txtAddress);
status = findViewById(R.id.txtStatus);
recentTemp = findViewById(R.id.txtRecentTemp);
tempMax = findViewById(R.id.txtTempMax);
tempMin = findViewById(R.id.txtTempMin);
date = findViewById(R.id.txtDate);
description = findViewById(R.id.txtDescription);
image = findViewById(R.id.weatherIcon);
mainView = findViewById(R.id.mainView);
sunSet = findViewById(R.id.txtSunset);
sunRise = findViewById(R.id.txtSunrise);
windSpeed = findViewById(R.id.txtWindSpeed);
pressure = findViewById(R.id.txtPressure);
humidity = findViewById(R.id.txtHumidity);
feelsLike = findViewById(R.id.txtFeelsLike);
feelImg = findViewById(R.id.feelImg);
sunRiseImg = findViewById(R.id.sunriseImg);
sunSetImg = findViewById(R.id.sunsetImg);
windImg = findViewById(R.id.windImg);
pressureImg = findViewById(R.id.pressureImg);
humidityImg = findViewById(R.id.humidityImg);
sunRiseImg.setImageDrawable(getResources().getDrawable(R.drawable.iconsunrise));
sunSetImg.setImageDrawable(getResources().getDrawable(R.drawable.iconsunset));
pressureImg.setImageDrawable(getResources().getDrawable(R.drawable.iconpressure));
humidityImg.setImageDrawable(getResources().getDrawable(R.drawable.iconhumidity));
windImg.setImageDrawable(getResources().getDrawable(R.drawable.windspeed));
new weatherTask().execute();
btnSearch = findViewById(R.id.btnSearch);
etSearch = findViewById(R.id.etSearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager inputMethodManager= (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getRootView().getWindowToken(),0);
city = String.valueOf(etSearch.getText());
new weatherTask().execute();
mainView.requestFocus();
}
});
}
class weatherTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... strings) {
return HttpRequest.excuteGet("http://api.openweathermap.org/data/2.5/weather?q="+city+"&units=metric&appid="+API);
}
@Override
protected void onPostExecute(String s) {
try {
JSONObject jsonObject = new JSONObject(s);
JSONObject main = jsonObject.getJSONObject("main");
JSONObject sys = jsonObject.getJSONObject("sys");
JSONObject wind = jsonObject.getJSONObject("wind");
JSONObject weather = jsonObject.getJSONArray("weather").getJSONObject(0);
String location = jsonObject.getString("name") + ", " + sys.getString("country");
String shortDes = weather.getString("description");
String txtDate = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).format(new Date(jsonObject.getLong("dt") * 1000)) +"";
String icon = weather.getString("icon");
setImage(image, icon);
NumberFormat numberFormat = NumberFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(1);
String txtTemp = numberFormat.format(main.getDouble("temp")) +"°C";
String txtTempMax = numberFormat.format(main.getDouble("temp_max")) +"°C";
String txtTempMin = numberFormat.format(main.getDouble("temp_min")) +"°C";
String txtFeelLike = numberFormat.format(main.getDouble("feels_like")) + "°C";
String descriptionDetail = "Today: " + shortDes +" conditions with a heat index of " +txtFeelLike +". The high will be " + txtTempMax +".";
if(main.getDouble("feels_like") < 15){
feelImg.setImageDrawable(getResources().getDrawable(R.drawable.feelcold));
}else if (main.getDouble("feels_like") > 30){
feelImg.setImageDrawable(getResources().getDrawable(R.drawable.feelhot));
}else{
feelImg.setImageDrawable(getResources().getDrawable(R.drawable.happiness));
}
String txtPressure = main.getString("pressure") + "hPa";
String txtHumidity = main.getString("humidity") + "%";
String txtWind = wind.getString("speed") + "km/h";
String txtSunrise = new SimpleDateFormat("hh:mma", Locale.ENGLISH).format(new Date(sys.getLong("sunrise") * 1000));
String txtSunset = new SimpleDateFormat("hh:mma", Locale.ENGLISH).format(new Date(sys.getLong("sunset") * 1000));
address.setText(location);
status.setText(shortDes);
date.setText(txtDate);
recentTemp.setText(txtTemp);
tempMax.setText("Max: "+txtTempMax);
tempMin.setText("Min: "+txtTempMin);
description.setText(descriptionDetail);
sunRise.setText(txtSunrise);
sunSet.setText(txtSunset);
pressure.setText(txtPressure);
humidity.setText(txtHumidity);
windSpeed.setText(txtWind);
feelsLike.setText(txtFeelLike);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void setImage(final ImageView imageView, final String value){
runOnUiThread(new Runnable() {
@Override
public void run() {
switch (value){
case "01d":
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i01d));
mainView.setBackground(getResources().getDrawable(R.drawable.cloudday));
break;
case "01n":
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i01n));
mainView.setBackground(getResources().getDrawable(R.drawable.cloudnight));
break;
case "02d":
mainView.setBackground(getResources().getDrawable(R.drawable.cloudday));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i02d));
break;
case "02n":
mainView.setBackground(getResources().getDrawable(R.drawable.cloudnight));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i02n));
break;
case "03d":
mainView.setBackground(getResources().getDrawable(R.drawable.scatteredcloud));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i03d));
break;
case "03n":
mainView.setBackground(getResources().getDrawable(R.drawable.cloudnight));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i03d));
break;
case "04d":
mainView.setBackground(getResources().getDrawable(R.drawable.brokenclouds));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i04d));
break;
case "04n":
mainView.setBackground(getResources().getDrawable(R.drawable.cloudnight));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i04d));
break;
case "09d":
mainView.setBackground(getResources().getDrawable(R.drawable.showerrain));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i09d));
break;
case "09n":
mainView.setBackground(getResources().getDrawable(R.drawable.showerrain));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i09n));
break;
case "10d":
mainView.setBackground(getResources().getDrawable(R.drawable.rainday));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i10d));
break;
case "10n":
mainView.setBackground(getResources().getDrawable(R.drawable.rainnight));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i10n));
break;
case "11d":
mainView.setBackground(getResources().getDrawable(R.drawable.thunderstorm));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i11d));
break;
case "11n":
mainView.setBackground(getResources().getDrawable(R.drawable.thunderstorm));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i11n));
break;
case "13d":
mainView.setBackground(getResources().getDrawable(R.drawable.snowday));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i13d));
break;
case "13n":
mainView.setBackground(getResources().getDrawable(R.drawable.snownight));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i13n));
break;
case "50d":
mainView.setBackground(getResources().getDrawable(R.drawable.mistday));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i50d));
break;
case "50n":
mainView.setBackground(getResources().getDrawable(R.drawable.mistnight));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.i50n));
break;
}
}
});
}
package com.example.project\u天气预报;
导入androidx.appcompat.app.ActionBar;
导入androidx.appcompat.app.appcompat活动;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.view.inputmethod.InputMethodManager;
导入android.widget.EditText;
导入android.widget.ImageButton;
导入android.widget.ImageView;
导入android.widget.TextView;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.text.NumberFormat;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.Locale;
公共类MainActivity扩展了AppCompatActivity{
字符串API=“API:5f0e7bb0ea868b77d281b07c046f565c”;
字符串城市=“河内”;
TextView地址、状态、最近温度、最大温度、最小温度、日期、说明、日落、日出、,
风速、压力、湿度、感觉;
ImageView图像、sunSetImg、Sunseimg、windImg、pressureImg、humidityImg、feelImg;
图像按钮搜索;
编辑文本搜索;
主视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ActionBar=getSupportActionBar();
actionBar.hide();
地址=findViewById(R.id.txtAddress);
状态=findViewById(R.id.txtStatus);
recentTemp=findViewById(R.id.txtRecentTemp);
tempMax=findviewbyd(R.id.txtempmax);
tempMin=findviewbyd(R.id.txtempmin);
日期=findViewById(R.id.txtDate);
描述=findViewById(R.id.txtDescription);
image=findviewbyd(R.id.weatherIcon);
mainView=findViewById(R.id.mainView);
日落=findViewById(R.id.txtSunset);
日出=findViewById(R.id.txtSunrise);
风速=findViewById(R.id.txtWindSpeed);
压力=findViewById(R.id.TXT压力);
湿度=findViewById(R.id.txt湿度);
feelsLike=findviewbyd(R.id.txtfeelsike);
feelImg=findviewbyd(R.id.feelImg);
sunRiseImg=findViewById(R.id.sunRiseImg);
sunSetImg=findViewById(R.id.sunSetImg);
windImg=findViewById(R.id.windImg);
pressureImg=findViewById(R.id.pressureImg);
humidityImg=findViewById(R.id.humidityImg);
setImageDrawable(getResources().getDrawable(R.drawable.iconsunrise));
setImageDrawable(getResources().getDrawable(R.drawable.iconsunset));
pressureImageDrawable(getResources().getDrawable(R.drawable.iconpressure));
humidityImg.setImageDrawable(getResources().getDrawable(R.drawable.iconhumidity));
setImageDrawable(getResources().getDrawable(R.drawable.windspeed));
新建weatherTask().execute();
btnSearch=findviewbyd(R.id.btnSearch);
etSearch=findviewbyd(R.id.etSearch);
btnSearch.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
InputMethodManager InputMethodManager=(InputMethodManager)getSystemService(Activity.INPUT\u方法\u服务);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getRootView().getWindowToken(),0);
city=String.valueOf(etSearch.getText());
新建weatherTask().execute();
requestFocus();
}
});
}
类weatherTask扩展了AsyncTask{
@凌驾
受保护的字符串背景(字符串…字符串){
返回HttpRequest.excuteGet(“http://api.openweathermap.org/data/2.5/weather?q=“+city+”&units=metric&appid=“+API”);
}
@凌驾
受保护的void onPostExecute(字符串s){
试一试{
JSONObject JSONObject=新的JSONObject;
JSONObject main=JSONObject.getJSONObject(“main”);
JSONObject sys=JSONObject.getJSONObject(“sys”);
JSONObject wind=JSONObject.getJSONObject(“风”);
JSONObject weather=JSONObject.getJSONArray(“天气”).getJSONObject(0);
字符串位置=jsonObject.getString(“名称”)+,“+sys.getString(“国家”);
String shortDes=weather.getString(“说明”);
字符串txtDate=newsimpledateformat(“dd/MM/yyyy”,Locale.ENGLISH).format(newdate(jsonObject.getLong(“dt”)*1000))+“”;
字符串图标=weather.getString(“图标”);
设置图像(图像、图标);
NumberFormat NumberFormat=NumberFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(1);
字符串txtemp=numberFormat.format(main.getDouble(“temp”))+“摄氏度”;
字符串txtempmax=numberFormat.format(main.getDouble(“temp_max”))+“°C”;
字符串txtempmin=numberFormat.format(main.getDouble(“temp_min”))+“C”;
String txtFeelLike=numberFormat.format(main.getDouble(“感觉像”))+“C”;
String descriptionDetail=“今天:“+shortDes+”热指数为“+txtFeelLike+”的条件。最高值为“+txtempmax+”;
if(main.getDouble(“感觉像”)<15){
setImageDrawable(getResources().getDrawable(R.drawable.feelcold));
}else if(main.getDouble(“feels_like”)>30){
setImageDrawable(getResources().getDrawable(R.drawable.feelhot));
}否则{
feelImg.setImageDrawable(getResources().getDrawable(R.drawable.happiness));
}
字符串txtPressure=main.getString(“压力”)+“hPa”;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project_weatherforecast">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/cloudyy"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>