majdsalloum
majdsalloum

Reputation: 516

httppost cause the app to crash

i'm trying to send post request to my PHP page through java in android studio

first i tried httppost and the app crashed after pressing the key

then i read that i have to use new thread and i did so but still

the app crashes

here is the code :

 public void send () {
    final String name,phone,fb;
    EditText one,two,three;
    one=(EditText) findViewById(R.id.editText);
    two=(EditText) findViewById(R.id.editText2);
    three=(EditText) findViewById(R.id.editText3);
    name=one.getText().toString();
    phone=two.getText().toString();
    fb=three.getText().toString();
    Thread T=new Thread(new Runnable() {
        @Override
        public void run() {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://test.com/test.php");
            try {
                List <NameValuePair> nameValuePairs= new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("name",name));
                nameValuePairs.add(new BasicNameValuePair("number",phone));
                nameValuePairs.add(new BasicNameValuePair("fbname",fb));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response= httpclient.execute(httppost);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
        }
    });
    T.start();
}

Log : http://pastebin.com/KQ9Y3rrG

Upvotes: 2

Views: 77

Answers (1)

Juan Cort&#233;s
Juan Cort&#233;s

Reputation: 21122

Make sure you have the permissions for communicating with the internet set in your manifest file. If not, it will throw a SecurityException and break your app.

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

Other than that, I can't see anything in your log that could be the cause an exception.

Update

After checking the code provided by the user the problem was coming from the fact that the onClick event was supposed to trigget the send() method while unknowingly defining it without the required View v parameter.

The solution (to this problem, this doesn't mean it will make the httppost work) is simple to add the parameter to the definition of the method:

public void send (View v) {

Upvotes: 1

Related Questions