[英]Android HttpPost Black Screen
I am currently developing and App that calculates a few simple variables at the end of an activity and then uploads them to a server at the beginning of the next activity: 我目前正在开发一个App,该App在活动结束时会计算一些简单的变量,然后在下一个活动开始时将其上传到服务器:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.fairwell);
setTimer();
}
@Override
public void onResume(){
super.onResume();
is_active = true;
Log.d(TAG, "onResume enter FA");
//Download Variable Ids start
int WeightId = postmeasurements.getStatisticId(postmeasurements.mHCAW);
int BFPId = postmeasurements.getStatisticId(postmeasurements.mHCABFP);
int HydId = postmeasurements.getStatisticId(postmeasurements.mHCAHyd);
int MuscMass = postmeasurements.getStatisticId(postmeasurements.mHCAMM);
//Download Variable Ids End
Log.w("ANTApp", "WeightId is: " + WeightId);
Log.w("ANTApp", "BFPId is: " + BFPId);
Log.w("ANTApp", "HydId is: " + HydId);
Log.w("ANTApp", "MuscMass is: " + MuscMass);
//Post Measurements to Server Start
postmeasurements.postScaleMeasurements(WeightId, PostMeasurements.Weight);
postmeasurements.postScaleMeasurements(BFPId, PostMeasurements.BFP);
postmeasurements.postScaleMeasurements(HydId, PostMeasurements.Hyd);
postmeasurements.postScaleMeasurements(MuscMass, PostMeasurements.MuscMass);
//Post Measurements to Server End
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 112.0f;
getWindow().setAttributes(lp);
setTimer();
Log.d(TAG, "onResume exit FA");
}
The problem I am having here is that this causes a black screen between each activity that remains until all the measurements have been uploaded. 我在这里遇到的问题是,这会导致每个活动之间出现黑屏,直到所有测量值均已上传为止。 Each downloading and uploading method is run on its own background thread using Httppost:
每个下载和上传方法都使用Httppost在自己的后台线程上运行:
public void postScaleMeasurements(int StatId, double Value){
//int Id = 0;
String result = "";
//the serial data to send
final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("s", mDeviceId));
nameValuePairs.add(new BasicNameValuePair("statisticId", Integer.toString(StatId)));
nameValuePairs.add(new BasicNameValuePair("userId", Integer.toString(mHCAId)));
nameValuePairs.add(new BasicNameValuePair("statisticValue", Double.toString(Value)));
InputStream is = null;
Thread PostScaleData = new Thread(){
public void run(){
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://www.website.com/app/postStatisticValue.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
streamresponse = entity.getContent();
}catch(Exception e){
Log.e(TAG, "Error in http connection (verifytablet) "+e.toString());
}
}
};
PostScaleData.start();
try {
PostScaleData.join(10000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
and I am still receiving a black screen of approximatly 1 - 4 seconds. 而且我仍然收到大约1-4秒的黑屏。 What I really want is for the layout drawing process to finish before any measurements are uploaded.
我真正想要的是在上载任何尺寸之前完成布局图绘制过程。
Thank you all in advance for your amazing support. 预先感谢大家的大力支持。
在Worker Thread中编写您的Network Upload工作,最好使用AsyncTask 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.