public class MainActivity extends AppWidgetProvider
{
TextView tv;
RemoteViews views;
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context,appWidgetManager,appWidgetIds);
for(int i=0; i<appWidgetIds.length; i++){
int currentWidgetId = appWidgetIds[i];
views = new RemoteViews(context.getPackageName(),R.layout.activity_main);
appWidgetManager.updateAppWidget(currentWidgetId,views);
new PostTask().execute("url");
}
}
private class PostTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String url = params[0];
// Dummy code
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(1, TimeUnit.MINUTES); // connect timeout
client.setReadTimeout(1, TimeUnit.MINUTES); // socket timeout
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "data=something");
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "7a4d5df8-5aed-19bf-e1fb-c85f821c1d10")
.addHeader("content-type", "application/x-www-form-urlencoded")
.build();
Response response = null;
try {
response = client.newCall(request).execute();
return response.body().string();
} catch (Exception e1) {
e1.printStackTrace();
return e1.toString();
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String data = "";
try {
JSONObject jsonRootObject = new JSONObject(result);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("response_data");
//Iterate the jsonArray and print the info of JSONObjects
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int id = Integer.parseInt(jsonObject.optString("CDRId").toString());
String name = jsonObject.optString("Status").toString();
data += "Agent : " + (i + 1) + "\nCDRId : " + id + " \n Status : " + name + " \n ";
}
views.setTextViewText(R.id.tv,data);
} catch (Exception e) {
views.setTextViewText(R.id.tv,e.toString());
}
}
}
}
I am trying to get a part of JSON(that I have parsed previously) in a widget. I am using AsyncTask to separate it from main thread and I am using OkHttpClient library to get JSON. I have class PostTask that gets me the parsed JSON. But can please anyone tell me how can I display it in the widget. This is the code.
Create a custom event, something like this:
public class MyEvent {
private String text;
public MyEvent(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text= text;
}
}
Now at the place where you're posting the Event, simply create this custom Event with the json you want to send to your widget. (put this code in onPostExecute
).
EventBus.getDefault().post(new MyEvent(result);
Now simply wherever your textView is, in Activity or Fragment, register the eventBus in onCreate
:
EventBus.getDefault().register(this);
And create a method that listens for the event like this:
@Subscribe
public void onMyEvent(MyEvent myEvent){
String text = myEvent.getText();
//Now you can parse this text, if it's JSON, or you can simply set it
//to your textView or whatever
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.