0

I have and application in iOS and I'm sending data and it is working fine while this is the fist application for me in the Android and I'm trying to send data to the same server but I getting an issue.

I have searched a lot in Google but unfortunately I end up with this; What could be my issue?

MyActivity.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.graphics.BitmapFactory;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.EditText;

My code for sending the data

class btnSendClicker implements Button.OnClickListener
    {
        @Override
        public void onClick(View v) {
            {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://myserver.com/form/feedback-mailer.php");
                    try {
                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);

                        nameValuePairs.add(new BasicNameValuePair("name", "ab"));
                        nameValuePairs.add(new BasicNameValuePair("email", "abc@abc.com"));
                        nameValuePairs.add(new BasicNameValuePair("message", "abcd"));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        httpclient.execute(httppost);

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

My permissions in AndriodManifest.xml

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

Log issue:

7-27 15:23:33.452    1245-1245/com.example.bitiz8.myapplicationtest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.ABC.myapplicationtest, PID: 1245
    android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
            at java.net.InetAddress.getAllByName(InetAddress.java:214)
            at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
            at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
            at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
            at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
            at com.example.bitiz8.myapplicationtest.MyActivity$btnSendClicker.onClick(MyActivity.java:137)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
Luai Kalkatawi
  • 1,492
  • 5
  • 25
  • 51

1 Answers1

0

You must not call webservice in the UI thread. You can use Thread or Asynctask but be careful with asynctask and its memory leak.

Thread thread = new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://myserver.com/form/feedback-mailer.php");
                try {
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);

                    nameValuePairs.add(new BasicNameValuePair("name", "ab"));
                    nameValuePairs.add(new BasicNameValuePair("email", "abc@abc.com"));
                    nameValuePairs.add(new BasicNameValuePair("message", "abcd"));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    httpclient.execute(httppost);

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

thread.start();

Enjoy

mrroboaat
  • 5,602
  • 7
  • 37
  • 65