Android Studio 从小部件中的 web 获取 txt 文件



我是android工作室的新手,只想让一个应用程序做一件事:检查bytewerk是否打开(域stats.bytewerk.org/status.txt只有一个单词打开或关闭(并将其显示在小部件上。但是我尝试了各种httpconections,如凌空或okhttp,但从未得到结果。我已经在清单中添加了使用权限。

1:

public class StatusWidget<appWidgetId> extends AppWidgetProvider {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) throws IOException {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.status_widget);
new GetNotePadFileFromServer().execute();
//views.setViewVisibility(R.id.bytewerk_online, 0);
appWidgetManager.updateAppWidget(appWidgetId, views);
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
try {
updateAppWidget(context, appWidgetManager, appWidgetId);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}public class GetNotePadFileFromServer extends AsyncTask<Void, Void, Void> {
String TextHolder;
protected Void doInBackground(Void... params) {
try {
URL url = new URL("http://stats.bytewerk.org/status.txt");
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(url.openStream()));
TextHolder = "";
String TextHolder2 = "";
while ((TextHolder2 = bufferReader.readLine()) != null) {
TextHolder += TextHolder2;
}
bufferReader.close();
} catch (MalformedURLException malformedURLException) {
// TODO Auto-generated catch block
malformedURLException.printStackTrace();
TextHolder = malformedURLException.toString();
} catch (IOException iOException) {
// TODO Auto-generated catch block
iOException.printStackTrace();
TextHolder = iOException.toString();
}
return null;
}
@Override
protected void onPostExecute(Void finalTextHolder) {
textView.setText(TextHolder);
super.onPostExecute(finalTextHolder);
}

阿拉伯数字:

final TextView textView = (TextView) findViewById(R.id.text);
// ...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
textView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
textView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);

以及官方网站上的其他示例 当然我已经实现了

dependencies {
...
implementation 'com.android.volley:volley:1.1.1'
}

为您的Manifest.xml和凌空工作添加互联网权限,这一行:

<uses-permission android:name="android.permission.INTERNET" />

在标记application之前。

希望这有帮助。

这是我的解决方案:

static void updateAppWidget(Context context, final AppWidgetManager appWidgetManager,
final int appWidgetId) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.status_widget);
// ...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
String url ="http://stats.bytewerk.org/status.txt";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
public void onResponse(String response) {
// Display the first 500 characters of the response string.
if (response.equals("open")) {
views.setViewVisibility(R.id.status_online, 1);
} else {
views.setViewVisibility(R.id.status_offline, 1);
}
//views.setTextViewText(R.id.text, "Yeah!" + response);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
views.setViewVisibility(R.id.status_nointernet, 1);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
});
queue.add(stringRequest);

最新更新