gpt4 book ai didi

java - 如何在不显示黑屏的情况下掩盖来自android服务器的响应所花费的时间?

转载 作者:太空宇宙 更新时间:2023-11-03 10:37:15 25 4
gpt4 key购买 nike

我有一个请求响应 Activity ,其中来自服务器的响应用于扩展动态布局,我的问题是第一个请求来得很快,第二个请求有延迟时间,所以有时屏幕会变黑。我已经试过这个link , 但帮助不大

下面是我现在使用的代码。

 ClientResource clientResource=new ClientResource(IP.IP+"/api/device/view_device/"+r_no);
// ClientResource clientResource=new ClientResource("http://10.30.10.243:8080/api/device/view_device/101");

clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,username, password);
clientResource.get();
Response response= clientResource.getResponse();
System.out.println("Response from server is ----> "+response.getEntityAsText());
text=response.getEntityAsText().toString();
try {
JSONObject obj = new JSONObject(text);
JSONArray jArray = new JSONArray((String) obj.get("message"));
System.out.println("\n String is\t"+jArray+"\t\t");
len=jArray.length();
System.out.println("Length of Array is\t"+len);
for (int i=0;i<len;i++){
String dname=jArray.getJSONObject(i).getString("device_name").toUpperCase();
port= (String) jArray.getJSONObject(i).get("device_port");
Rno=jArray.getJSONObject(i).getString("room_no");
dev_id=(String)jArray.getJSONObject(i).getString("device_id");
String d_stat=(String)jArray.getJSONObject(i).get("device_control");
cList.add(d_stat);
list.add(dname);
lis.add(port);
stat.add(dev_id);
}
} catch (JSONException e) {
e.printStackTrace();
}


if (len==0){Toast.makeText(Test.this, "No Devices Found For This Room", Toast.LENGTH_SHORT).show();
/* Intent intent1=new Intent(Test.this,Landing_page.class);
finish();
startActivity(intent1);
*/
LinearLayout r1 = new LinearLayout(this);
r1.setBackgroundResource(R.drawable.border);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT,90);
r1.setPadding(0,10, 0, 0);
r1.setLayoutParams(lp);

TextView tv1 = new TextView(this);
tv1.setTextColor(getResources().getColor(R.color.black));
tv1.setText(" No Devices Found");
tv1.setTextSize(30);
tv1.setPadding(10,10,0,0);
LinearLayout.LayoutParams tv1params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(tv1params);
tv1.setGravity(View.TEXT_ALIGNMENT_CENTER);
r1.addView(tv1);

ll1.addView(r1);

}
try{
ClientResource clientResource1=new ClientResource(IP.IP+"/api/resource/device_status/"+r_no+"/"+dev_id);



System.out.println(IP.IP+"/api/resource/device_status/"+r_no+"/"+dev_id);

//clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC, username, password);
clientResource1.get();
Response response1= clientResource1.getResponse();
System.out.println("Response from status server is ----> "+response1.getEntityAsText());
String txt=response1.getEntityAsText().toString();


try {
JSONObject obj = new JSONObject(txt);
JSONArray jArray = new JSONArray((String) obj.get("message"));
System.out.println("\n String is\t"+jArray+"\t\t");
len1=jArray.length();
System.out.println("Length of Second Array is\t"+len1);
for (int i=0;i<len1;i++){
String dstat=jArray.getJSONObject(i).getString("Soft_status");
String istat=jArray.getJSONObject(i).getString("Inten_value");
list1.add(dstat);
list2.add(istat);
}
} catch (JSONException e) {
e.printStackTrace();
}}catch(Exception e){e.printStackTrace();}
/* if (len1==0){
progress.dismiss();
Toast.makeText(getApplicationContext(),"Device Status not Obtained",Toast.LENGTH_LONG);
list1.add("0");list1.add("0");list1.add("0");list1.add("0");list1.add("0");list1.add("0");list1.add("0");list1.add("0");
list2.add("2");list2.add("2");list2.add("2");list2.add("5");list2.add("2");list2.add("2");list2.add("2");list2.add("2");
}else{
progress.dismiss();

Toast.makeText(getApplicationContext(),"Devices Discovered",Toast.LENGTH_LONG);
}

*/
if (len1>0&&len>0){
progress.dismiss();
}

System.out.println(cList.toString().toUpperCase());

/* LinearLayout.LayoutParams.WRAP_CONTENT*/
for (int i = 0; i <list.size(); i++) {

cnid=stat.get(i);

LinearLayout layout = new LinearLayout(this);
layout.setBackgroundResource(R.drawable.border);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// params.setMargins(100, 15, 50, 0);

layout.setLayoutParams(params);

layout.setOrientation(LinearLayout.HORIZONTAL);

layout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
layout.setPadding(30, 10, 0, 30);

System.out.println(list);


ImageView iv[];
iv=new ImageView[len];
iv[i]=new ImageView(this);
String buffer=list.get(i);
if (list1.get(i).equals("1")){
flag=1;
iv[i].setImageResource((Integer)dm.get(""+buffer.replace(" ","").toUpperCase()+""));
}else if (list1.get(i).equals("0")){ flag=0;
iv[i].setImageResource((Integer) hm.get(""+buffer.replace(" ","").toUpperCase()+""));}
iv[i].setId(i+100);
iv[i].setTag(list);
LinearLayout.LayoutParams ivparams = new LinearLayout.LayoutParams
(60, LinearLayout.LayoutParams.MATCH_PARENT);
iv[i].setPadding(10,0,0,0);
// ivparams.setMargins(0,0,0,0);
iv[i].setLayoutParams(ivparams);
if(list.get(i).equals("BULB")||list.get(i).equals("LED1")||list.get(i).equals("FAN"))
{
String d=list.get(i);
System.out.println("\n\t\t:: Device Controllable ::" +i+
"\n");
iv[i].setOnClickListener(HandleOnClick(d,iv[i],iv[i].getId()));
}else if (list.get(i).equals("CFL")||list.get(i).equals("LED")){
System.out.println("Device not Controllabe:: "+i);
}

layout.addView(iv[i]);


TextView tv = new TextView(this);
LinearLayout.LayoutParams tvparams = new LinearLayout.LayoutParams
(90, LinearLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(tvparams);
tv.setPadding(27,27,27,27); //tv.setPadding(27,27,27,27);
tvparams.setMargins(30, 0, 0, 0);
tv.setTextSize(15);
tv.setTextColor(getResources().getColor(R.color.black));
//tv.setPadding(10,10,10,10);


tv.setText(list .get(i));
layout.addView(tv);



ToggleButton tb[];
tb=new ToggleButton[len];
tb[i]=new ToggleButton(this);

LinearLayout.LayoutParams tbparams = new LinearLayout.LayoutParams
(60, 40);
tbparams.setMargins(200, 5, 0, 0);
tb[i].setBackgroundResource(R.drawable.bgtoggle);
tb[i].setId(i);
tb[i].setTextOff("off");
tb[i].setTextOn("on");
tb[i].setTextColor(getResources().getColor(R.color.transparent));
tb[i].setTag(list.get(i));
//tb[i].setPadding(4, 4, 4, 4);
tb[i].setLayoutParams(tbparams);
if (list1.get(i).equals("1")){
tb[i].setChecked(true);
}else if(list1.get(i).equals("0")) {
flag=0;
tb[i].setChecked(false);
}

tb[i].setOnCheckedChangeListener(handleOnClick(
tb[i], tb[i].getId(),tb[i].getTag(),list ,lis,stat,iv[i],iv[i].getTag()));
layout.addView(tb[i]);

//Image View Loop
ImageView Iv[];
Iv=new ImageView[len];
Iv[i]=new ImageView(this);
Iv[i].setId(i+1);
//ImageView Iv = new ImageView(context);
Iv[i].setImageResource(R.drawable.enrgy);
LinearLayout.LayoutParams Ivparams = new LinearLayout.LayoutParams
(60, 50);
Ivparams.setMargins(150, 25, 0, 0);
Iv[i].setLayoutParams(Ivparams);
Iv[i].setOnClickListener(handleOnClick(Iv[i],r_no,dev_id,Iv[i].getId(),cntr,tb[i].getId()));
layout.addView(Iv[i]);


ll1.addView(layout);

}

请指导。

最佳答案

你可以使用异步任务来做到这一点......

private class ClassName extends AsyncTask<Void, Void, Void>
{
ProgressDialog pd = new ProgressDialog(SignUp.this);

@Override
protected void onPreExecute()
{
pd.setMessage("Please wait..");
pd.setCancelable(false);
pd.show();
}

@Override
protected Void doInBackground(Void... arg0)
{
..................................
//your code for json calling -- Server call 1
..................................
//use runOnUi thread to update ui
..................................

runOnUiThread(new Runnable() {
@Override
public void run() {
// WORK on UI thread here
}
});

..................................
//your code for json calling -- Server call 2
..................................
//use runOnUi thread to update ui
..................................

runOnUiThread(new Runnable() {
@Override
public void run() {
// WORK on UI thread here
}
});



return null;
}

@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);

if(pd.isShowing())
pd.dismiss();
}

关于java - 如何在不显示黑屏的情况下掩盖来自android服务器的响应所花费的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41499065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com