- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个请求响应 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/
我一直在读一本分配给类(class)的书,它提到数组访问需要 O(1) 时间。我意识到这非常快(也许尽可能快),但是如果您有一个循环必须多次引用它,那么分配一个临时变量以在数组中查找值有什么好处吗?或
我一直试图找出为什么这个查询花了这么长时间。以前,它的执行时间约为 150 毫秒到 200 毫秒,但现在需要 25 秒或更长时间。这是从昨晚到今天之间的事。唯一改变的就是将数据添加到表中。 根据下面的
我有一个 ng repeat 重复数据。 - data.image(src)部分为null,src=null的不再重复。 我用一个简单的 ng-if 解决了它。
我有一个包含大量测试的 Laravel 项目。我正在使用 pcov 来计算代码覆盖率,大约需要 4 分钟。但是 pcov 不支持分支覆盖,所以我决定使用 xdebug。 使用 xdebug 测试执行,
我已经被这个问题困扰了一段时间了,我被难住了。 Automapper 需要 4 秒来映射 19 个对象。在我的机器(24GB 内存,3.6Ghz i7)上,该操作应该花费毫秒或纳秒。 这是映射调用。
我有一个包含大量测试的 Laravel 项目。我正在使用 pcov 来计算代码覆盖率,大约需要 4 分钟。但是 pcov 不支持分支覆盖,所以我决定使用 xdebug。 使用 xdebug 测试执行,
我在机器 A 上有一个 java 进程通过 TCP 与机器 B 上的 Tomcat 通信。 TCP 连接(只是 syn-syn/ack 交换)始终需要 100 毫秒的数量级,而 ping 请求需要 1
我做了一项任务,从 sqlserver 获取超过 200 万条记录并将它们填充到 Asp.net GridView 中。 问题是,查询需要超过 2 分钟才能获得记录,而我的查询现在已经完全优化。 当我
我希望将 165 秒变成 2:40 而不是 0:2:45 函数需要能够适应秒值的大小。 我知道有无数种方法可以做到这一点,但我正在寻找一种干净的方法来做到这一点,除了 jQuery 之外没有任何外部库
我是一名优秀的程序员,十分优秀!