0

I have my code to post details to server but any time I call the method the application stops!

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

}

when I comment the code to post the 'toast' that appears before the code works pretty well but once I call the whole method, the application collapses.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

1 Answers1

1

Two possibilities:

  1. This is running in background thread.

  2. One of the variables in that line is null

Patrick Chan
  • 1,019
  • 10
  • 14
  • When I assign values directl like: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")); the error persists – user4328432 Dec 05 '14 at 12:03
  • If you are not running this in background thread, you are encountering NetworkOnMainThreadException. Network interaction is not allowed on UI thread. It means you should run this method in AsyncTask. – Patrick Chan Dec 05 '14 at 12:11