Asad Raza
Asad Raza

Reputation: 33

Error at httpclient.execute(httppost);

I have developed an application to send data to a server but when I try to run it, I am getting errors at execution of http request....

Here is the code...

    public void send()
    {
        // get the message from the message text box
        String msg = msgTextField.getText().toString();  
        System.out.println("I am here!!!");
        // make sure the fields are not empty
        if (msg.length()>0)
        {

            Toast.makeText(this, "started", Toast.LENGTH_LONG).show();
            DefaultHttpClient httpclient=new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.fblocation.asia/update/index.php");
           List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
           nameValuePairs.add(new BasicNameValuePair("lat", Double.toString(clat)));
           nameValuePairs.add(new BasicNameValuePair("long", Double.toString(clong)));
           nameValuePairs.add(new BasicNameValuePair("username", msg));
         try {
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
           httpclient.execute(httppost);
           Toast.makeText(this, "sent", Toast.LENGTH_LONG).show();
         } catch (Exception e) {
             Toast t=Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG);
             t.show();
         }

        }
        else
        {
            // display message if text fields are empty
            Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
        }

    }

Please give me some suggestions.

Thank you!

Upvotes: 2

Views: 9310

Answers (2)

onkar
onkar

Reputation: 4547

Please try this code

HttpClient httpclient = new DefaultHttpClient();
        String responseStr="";
        String URL=Constants.API_URL+"details/";
        HttpPost httppost = new HttpPost(URL);

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("id", pick_up_id));
            nameValuePairs.add(new BasicNameValuePair("username", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            int responseCode = response.getStatusLine().getStatusCode();
            switch(responseCode) {
            case 200:
                HttpEntity entity = response.getEntity();
                if(entity != null) {
                    String responseBody = EntityUtils.toString(entity);
                    responseStr=responseBody;
                }
                break;
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        System.out.println("this is response "+responseStr);

Upvotes: 3

bofredo
bofredo

Reputation: 2348

you have to do http-calls asynchronous. that means some major changes for your code and some days of practicing. follow the links from the comments to get a first impression of AsyncTask.

also this guide is pretty solid too!!

Upvotes: 0

Related Questions