java - Android 代码发布到服务器时出错

标签 java android json

我有代码将详细信息发布到服务器,但每当我调用该方法时,应用程序就会停止!

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();
        }

}

当我评论代码以发布“toast”时,代码运行得很好,但是一旦我调用整个方法,应用程序就会崩溃。

最佳答案

两种可能性:

  1. 它在后台线程中运行。

  2. 该行中的一个变量为 null

关于java - Android 代码发布到服务器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27314654/

相关文章:

android - 单行填充父级ListView高度

android - 来自智能手机摄像头的基于网络的实时视频流

javascript - 将 Json 数组合并到对象

javascript - 如何将表单字段转换为 Json 对象?

java - 从 JTable 解析的 double 总和

java - 如何使用java实现expect "interact"命令

android - 如何使用 fragment 使 viewpager 快速转换?

ruby-on-rails - 提取可能是也可能不是数组的哈希键

java - 如何拦截java中的方法

java - 如何为我的 Web 应用程序创建具有自动完成功能的所见即所得代码编辑器?