2013-08-17 48 views
0

我目前正在開發和應用,計算在活動結束幾個簡單的變量,然後在下次活動開始其上傳到服務器:的Android HttpPost黑屏

@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"); 
    } 

這個問題我我在這裏是這會導致每個活動之間的黑屏,直到所有的測量結果都被上傳。每個下載和上傳方法是使用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(); 
    } 

,我仍然接收approximatly 1黑屏 - 4秒。我真正想要的是在上傳任何測量之前完成佈局繪製過程。

非常感謝大家的支持。

+0

您封鎖了主線程。 –

+0

嘗試使用AsyncTask。看看這個答案。它會幫助你。 http://stackoverflow.com/a/17969562/2571313 –

+0

調用AsyncTask onCreate()方法,以便您的主線程不會被阻止,並且您的上傳也可以在加載時進行。 –

回答

0

在工作線程中寫入您的網絡上傳工作,最好使用AsyncTask