2014-12-05 24 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

相关问题