-2

I am a beginner in android. I am trying to send an Arraylist to a php server from android application through Https. My code and error log is shown here.

    public class MakeRoti extends Activity{
private SeekBar thickness; 
private SeekBar roast; 
private SeekBar oil; 
private EditText numbers;

int thickness_val;
int roast_val;
int oil_val;

String no_rotis;


String thick;
String oilval;
String roastval;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.makeroti);
    thickness= (SeekBar)findViewById(R.id.thickness);
    thickness.setProgress(1);
    thickness.incrementProgressBy(1);
    thickness.setMax(5);
    roast= (SeekBar)findViewById(R.id.roast);
    roast.setProgress(1);
    roast.incrementProgressBy(1);
    roast.setMax(5);
    oil= (SeekBar)findViewById(R.id.oil);
    oil.setProgress(1);
    oil.incrementProgressBy(1);
    oil.setMax(3);
    numbers=(EditText)findViewById(R.id.numberval);

}
public void sendData(View view) 
{

    thickness_val=thickness.getProgress();;
     roast_val=roast.getProgress();
     oil_val=oil.getProgress();
     no_rotis=numbers.getText().toString();

     thick=Integer.toString(thickness_val);
    roastval=Integer.toString(roast_val);
     oilval=Integer.toString(oil_val);


            new LongOperation().execute();


}
private class LongOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("thickness", thick));
nameValuePairs.add(new BasicNameValuePair("roastval", roastval));
nameValuePairs.add(new BasicNameValuePair("oilval", oilval));
nameValuePairs.add(new BasicNameValuePair("number", no_rotis));



    String url="https://192.168.0.100/testhttps.php";
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

    DefaultHttpClient client = new DefaultHttpClient();

    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    registry.register(new Scheme("https", socketFactory, 443));
    SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
    DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

    // Set verifier     
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

    // Example send http request

    HttpPost httpPost = new HttpPost(url);
    try {

        HttpResponse response = httpClient.execute(httpPost);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block  
        e.printStackTrace();
    }

        return "";
    }

    protected void onPostExecute(String result) {

    }

    protected void onPreExecute() {}

    protected void onProgressUpdate(Void... values) {}
}

}

this is my whole code.. pls help..

My error log includes :

FATAL EXCEPTION: main

java.lang.IllegalStateException: Could not execute method of the activity

 Something Caused by: java.lang.reflect.InvocationTargetException
 Caused by: android.os.NetworkOnMainThreadException

1 Answers1

-1

NetworkOnMainThreadException: The exception that is thrown when an application attempts to perform a networking operation on its main thread.

You should call sendfeedback() method on AsyncTask then only above code will work. As webserver is taking lot of time to response main thread becomes unresponsive. To avoid it you should call it on another thread. Hence AsyncTask is better.

http://android-developers.blogspot.in/2009/05/painless-threading.html

Try below code:-

call your AsyncTask

new LongOperation().execute(thick,roast,oil,no_rotis);

your AsyncTask

private class LongOperation extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
    nameValuePairs.add(new BasicNameValuePair("thickness", params[0]));
    nameValuePairs.add(new BasicNameValuePair("roast", params[1]));
    nameValuePairs.add(new BasicNameValuePair("oil", params[2]));
    nameValuePairs.add(new BasicNameValuePair("number", params[3]));



        String url="https://192.168.0.100/testhttps.php";
        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

        DefaultHttpClient client = new DefaultHttpClient();

        SchemeRegistry registry = new SchemeRegistry();
        SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
        socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
        registry.register(new Scheme("https", socketFactory, 443));
        SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
        DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

        // Set verifier     
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

        // Example send http request

        HttpPost httpPost = new HttpPost(url);
        try {

            HttpResponse response = httpClient.execute(httpPost);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
        }
       }
            return "";
        }

        @Override
        protected void onPostExecute(String result) {

        }

        @Override
        protected void onPreExecute() {}

        @Override
        protected void onProgressUpdate(Void... values) {}
    }
duggu
  • 37,191
  • 12
  • 114
  • 111