我有一个请求响应活动,其中来自服务器的响应用于膨胀动态布局,我的问题是第一个请求来得快,第二个请求延迟时间,所以有时屏幕变黑.我已经尝试过这个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中的服务器而不显示黑屏的时间?全部内容,希望文章能够帮你解决java – 如何掩盖响应来自android中的服务器而不显示黑屏的时间?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)