0

This is my Java code:

 @Override
public void onClick(View v) {
     switch (v.getId()) {

  case R.id.button1:
      HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://iwindroids.ru/test.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            //nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("lal", "AndDev is Cool!"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

        } catch (ClientProtocolException e) {

            // TODO Auto-generated catch block
        } catch (IOException e) {

            // TODO Auto-generated catch block
        }
    break;
    default:
    break;

  }
}

When I press button1 writes:

Unfortunately, [App name] App has stopped.

I wrote in Manifest.

 <uses-permission android:name="android.permission.INTERNET"/>

Android. Eclipse. POST. Http

Aydomir
  • 13
  • 3

3 Answers3

1

You should not make http calls from main/UI thread. You should do in a separate thread in android. So use a new Thread or AsyncTask to make http calls and update the UI later

user1670443
  • 461
  • 2
  • 6
  • 17
  • Correct, but I would recommend an AsyncTask as it gives you more UI control with the `onPreExecute`, `onProgressUpdate`, `onPostExecute` methods. – Mark Buikema Feb 28 '14 at 09:14
0

create a AsyncTask class like below. And call the AsyncTask class whrere you want. In the AsyncTask class call the webservice postmethod in doInBackground Method

class asyncTaskPost extends AsyncTask<String, Void, Void> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

        }

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            // call the webservice post method here
            postMethod();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

        }
    }

And your webservice post method like below

public static void postMethod()
    {

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("place your url here");
        try {



            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("name", name));
            nameValuePairs.add(new BasicNameValuePair("phoneno", strMobileNumber));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // Execute HTTP Post Request
            HttpResponse response= httpclient.execute(httppost);
            HttpEntity responseEntity = response.getEntity();
            String serverResponse = EntityUtils.toString(responseEntity);
           Log.i("server response is="+serverResponse,"");

        } catch (ClientProtocolException e) {
            e.printStackTrace();

            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
Mr. N.V.Rao
  • 1,082
  • 13
  • 27
  • ----> `protected Void doInBackground(String... params) {` Multiple markers at this line - implements android.os.AsyncTask.doInBackground - This method must return a result of type Void – Aydomir Feb 28 '14 at 14:32
  • I forgoe to add the return statement in that method now check that one @Aydomir – Mr. N.V.Rao Mar 01 '14 at 05:08
0

You can use this THE BEST way : http://smartmobsolution.blogspot.in/2014/02/the-best-way-toaccess-data-from-web-in.html

Santhosh
  • 4,956
  • 12
  • 62
  • 90