2014-12-05 44 views
0

我有我的代碼發佈詳細信息到服務器,但任何時候我調用應用程序停止的方法!Android代碼錯誤發佈到服務器

public void postDetails() 
{ 

    Toast.makeText(BTPrinterDemo.this, "Am supposed to post"+longi+"..."+lati+" for device: "+imei, Toast.LENGTH_LONG).show(); 

    /* 
    * Posting coordinates to server 
    * 
    * */ 
     HttpClient httpcl = new DefaultHttpClient(); 
     HttpPost httppst = new HttpPost("https://xxxxxxxxxxx/myfile/geo"); 
     //geo is a JSON file on server      
     try { 


      // Add your data 
      List<NameValuePair> nameValuePairz = new ArrayList<NameValuePair>(4); 
      nameValuePairz.add(new BasicNameValuePair("geo-type","geo..me")); 
      nameValuePairz.add(new BasicNameValuePair("long",longi)); 
      nameValuePairz.add(new BasicNameValuePair("lat",lati)); 
      nameValuePairz.add(new BasicNameValuePair("imei",imei)); 
      // 
      httppst.setEntity(new UrlEncodedFormEntity(nameValuePairz)); 

      // Execute HTTP Post Request 
      HttpResponse response = httpcl.execute(httppst); 
      String responseBody0 = EntityUtils.toString(response.getEntity()); 
      if(responseBody0.trim().equals("")){ 
       Toast.makeText(ctx, "No Response. Check internet connectivity : "+responseBody0.trim(), Toast.LENGTH_LONG).show(); 
      }else{ 
       Toast.makeText(ctx, "Response : "+responseBody0.trim(), Toast.LENGTH_LONG).show(); 

       if(responseBody0.trim().equals("ERROR")){ 
        Toast.makeText(ctx, "An Error occured. Contact the administrator", Toast.LENGTH_LONG).show(); 
        return; 
       } 
       if(responseBody0.trim().equals("NONE")){ 
        Toast.makeText(ctx, "Login Invalid", Toast.LENGTH_LONG).show(); 
        return; 
       } 
        return; 
      } 


     } catch (ClientProtocolException e) { 
      Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show(); 
     } catch (IOException e) { 
      Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 

} 
當我評論的代碼發佈了「乾杯」的代碼工作得很好,之前出現,但一旦我把整個方法,應用程序崩潰

+0

你想顯示乾杯傳遞當前對象(活動或getApplicationContext()) – Thirumoorthy 2014-12-05 11:27:45

+1

嘗試增加你的logcat的輸出,可以幫助人們擺脫以確定您的問題。 – axierjhtjz 2014-12-05 11:31:02

+0

你得到什麼錯誤?你如何定義ctx? – Chris 2014-12-05 11:31:55

回答

1

兩種可能性:

  1. 這是在後臺線程中運行。

  2. 一個在該行的變量是空

+0

當我給值賦值directl如:nameValuePairz.add(new BasicNameValuePair(「geo-type」,「conductor」)); nameValuePairz.add(new BasicNameValuePair(「long」,「36.65454」)); nameValuePairz.add(new BasicNameValuePair(「lat」,「 - 1.1184」)); nameValuePairz.add(new BasicNameValuePair(「imei」,「357123456789369」));錯誤仍然存​​在 – user4328432 2014-12-05 12:03:26

+0

我也沒有任何線程初始化... – user4328432 2014-12-05 12:06:38

+0

如果你沒有在後臺線程中運行這個,你遇到了NetworkOnMainThreadException。 UI線程不允許網絡交互。這意味着你應該在AsyncTask中運行這個方法。 – 2014-12-05 12:11:59

相關問題