Okhttp 解析和 recyclerview 片段?
Okhttp parse and recyclerview fragment?
我在使用 Okhttp 之外的 list_state 时遇到问题,当我尝试将它传递给适配器时,结果为空。
我无法理解,因为 onResponse 内部使用的所有变量都不会传递到外部。
我试图在 onResponse 中设置一个 recyclerview 和适配器,但在它上面出现错误并且 ap 崩溃。
我使用的代码在下面任何人都可以帮助我吗?
对不起我的英语。
public class dashboard_device extends Fragment implements ListOwner{
RecyclerView mRecicleView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
ArrayList<String> lista_show;
ArrayList<String> lista_state = new ArrayList<String> ();
ArrayList<String> lista_prestate = new ArrayList<String> ();
String p,d;
String myResponse = null;
List<risposta_json> posts;
public dashboard_device() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View fragmentView = inflater.inflate (R.layout.dashboard_device_layout, container, false);
final ArrayList<String> lista_show = new ArrayList<String> ();
dashboard_DB db1 = new dashboard_DB (getContext ());
//recupero datbase DB
db1.open();
Cursor c = db1.ottieniTuttidati();
if (c.moveToFirst()) {
do {
lista_show.add (c.getString(2));
} while (c.moveToNext());
}
db1.close();
//fine recupero dati da db
// Inflate the layout for this fragment
GestioneDB db = new GestioneDB(getContext ());
db.open();
Cursor c1 = db.ottieniTuttidati();
if (c1.moveToFirst()) {
do {
p = c1.getString(1);
d = c1.getString(2);
} while (c1.moveToNext());
}
db.close();
//fine recupero dati da db
final String url = p;
String token=d;
OkHttpClient client = new OkHttpClient ();
Request request = new Request.Builder()
.url("https://"+url+"/api/states")
.addHeader("Authorization", "Bearer " + token)
.build();
client.newCall(request).enqueue(new Callback () {
@Override
public void onFailure(Call call, IOException e) {e.printStackTrace();}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
myResponse = response.body().string();
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
String jsonOutput = myResponse;
Type listType = new TypeToken<List<risposta_json>> (){}.getType();
posts = gson.fromJson(jsonOutput, listType);
// Log.d("MYRESPONSE", String.valueOf (myResponse));
//attributes = gson.fromJson (myResponse, risposta_json.class);
for (int i = 0; i<lista_show.size (); i++) {
for (int l = 0; l<posts.size ();l++) {
if (posts.get (l).getAttributes ().getFriendly_name ()!=null && posts.get (l).getAttributes ().getFriendly_name ().equalsIgnoreCase (lista_show.get (i))) {
lista_state.add (posts.get (l).getState ());
}
}
}
}
}
});
Log.d("STATI", String.valueOf (lista_state));
mRecicleView = container.findViewById (R.id.Reclycler_View);
mRecicleView.setHasFixedSize (true);
mLayoutManager = new GridLayoutManager (getContext (),3);
mAdapter = new adapter_dash (lista_show,lista_state, getContext (),dashboard_device.this);
mRecicleView.setLayoutManager (mLayoutManager);
mRecicleView.setAdapter (mAdapter);
runanimation1(mRecicleView,0);
Log.d("LISTA SHOW", String.valueOf (lista_show));
return fragmentView;
}
private void runanimation1(RecyclerView mRecicleView, int type) {
Context context=mRecicleView.getContext ();
LayoutAnimationController controller = null;
if(type==0)
controller = AnimationUtils.loadLayoutAnimation (context,R.anim.layout_animation);
mRecicleView.setLayoutAnimation (controller);
mRecicleView.getAdapter ().notifyDataSetChanged ();
mRecicleView.scheduleLayoutAnimation ();
}
@Override
public void push(ArrayList<String> list) {
}
}
使用解决
.getactivity().runOnUiThread(新的 Runnable)等等……等等……
我在使用 Okhttp 之外的 list_state 时遇到问题,当我尝试将它传递给适配器时,结果为空。 我无法理解,因为 onResponse 内部使用的所有变量都不会传递到外部。 我试图在 onResponse 中设置一个 recyclerview 和适配器,但在它上面出现错误并且 ap 崩溃。 我使用的代码在下面任何人都可以帮助我吗? 对不起我的英语。
public class dashboard_device extends Fragment implements ListOwner{
RecyclerView mRecicleView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
ArrayList<String> lista_show;
ArrayList<String> lista_state = new ArrayList<String> ();
ArrayList<String> lista_prestate = new ArrayList<String> ();
String p,d;
String myResponse = null;
List<risposta_json> posts;
public dashboard_device() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View fragmentView = inflater.inflate (R.layout.dashboard_device_layout, container, false);
final ArrayList<String> lista_show = new ArrayList<String> ();
dashboard_DB db1 = new dashboard_DB (getContext ());
//recupero datbase DB
db1.open();
Cursor c = db1.ottieniTuttidati();
if (c.moveToFirst()) {
do {
lista_show.add (c.getString(2));
} while (c.moveToNext());
}
db1.close();
//fine recupero dati da db
// Inflate the layout for this fragment
GestioneDB db = new GestioneDB(getContext ());
db.open();
Cursor c1 = db.ottieniTuttidati();
if (c1.moveToFirst()) {
do {
p = c1.getString(1);
d = c1.getString(2);
} while (c1.moveToNext());
}
db.close();
//fine recupero dati da db
final String url = p;
String token=d;
OkHttpClient client = new OkHttpClient ();
Request request = new Request.Builder()
.url("https://"+url+"/api/states")
.addHeader("Authorization", "Bearer " + token)
.build();
client.newCall(request).enqueue(new Callback () {
@Override
public void onFailure(Call call, IOException e) {e.printStackTrace();}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
myResponse = response.body().string();
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
String jsonOutput = myResponse;
Type listType = new TypeToken<List<risposta_json>> (){}.getType();
posts = gson.fromJson(jsonOutput, listType);
// Log.d("MYRESPONSE", String.valueOf (myResponse));
//attributes = gson.fromJson (myResponse, risposta_json.class);
for (int i = 0; i<lista_show.size (); i++) {
for (int l = 0; l<posts.size ();l++) {
if (posts.get (l).getAttributes ().getFriendly_name ()!=null && posts.get (l).getAttributes ().getFriendly_name ().equalsIgnoreCase (lista_show.get (i))) {
lista_state.add (posts.get (l).getState ());
}
}
}
}
}
});
Log.d("STATI", String.valueOf (lista_state));
mRecicleView = container.findViewById (R.id.Reclycler_View);
mRecicleView.setHasFixedSize (true);
mLayoutManager = new GridLayoutManager (getContext (),3);
mAdapter = new adapter_dash (lista_show,lista_state, getContext (),dashboard_device.this);
mRecicleView.setLayoutManager (mLayoutManager);
mRecicleView.setAdapter (mAdapter);
runanimation1(mRecicleView,0);
Log.d("LISTA SHOW", String.valueOf (lista_show));
return fragmentView;
}
private void runanimation1(RecyclerView mRecicleView, int type) {
Context context=mRecicleView.getContext ();
LayoutAnimationController controller = null;
if(type==0)
controller = AnimationUtils.loadLayoutAnimation (context,R.anim.layout_animation);
mRecicleView.setLayoutAnimation (controller);
mRecicleView.getAdapter ().notifyDataSetChanged ();
mRecicleView.scheduleLayoutAnimation ();
}
@Override
public void push(ArrayList<String> list) {
}
}
使用解决 .getactivity().runOnUiThread(新的 Runnable)等等……等等……