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

5
  • 1
    search for NetworkOnMainThreadException. Commented Feb 28, 2014 at 9:10
  • you doing this into Background Thread. Commented Feb 28, 2014 at 9:10
  • You are not catching the general Exception... Use it an you'll know what the problem is Commented Feb 28, 2014 at 9:10
  • 1
    can you add the error messages shown in your IDE ? Commented Feb 28, 2014 at 9:11
  • 2
    possible duplicate of android.os.NetworkOnMainThreadException Commented Feb 28, 2014 at 9:13

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Correct, but I would recommend an AsyncTask as it gives you more UI control with the onPreExecute, onProgressUpdate, onPostExecute methods.
0

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

Comments

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
        }
    }

2 Comments

----> protected Void doInBackground(String... params) { Multiple markers at this line - implements android.os.AsyncTask<java.lang.Void,java.lang.Void,java.lang.Void>.doInBackground - This method must return a result of type Void
I forgoe to add the return statement in that method now check that one @Aydomir

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.