1

My app works well for android 2.3.3 but it force closes in android 4.1.2 Below is my code to send data from android device to server.

HttpEntity entity;
HttpClient client = new DefaultHttpClient();
String url ="http://67.23.166.35:80/android/insertv2.php";

HttpPost request = new HttpPost(url);
StringEntity se = new StringEntity(datatoServer);
se.setContentEncoding("UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
entity = se;
request.setEntity(entity);
HttpResponse response = client.execute(request);
entity = response.getEntity();
Sanket Shah
  • 4,352
  • 3
  • 21
  • 41
user2686960
  • 25
  • 10

3 Answers3

1

Check following links buddy,

I am assuming that you are getting NetworkOnMainThreadException so you should use AsyncTask and RunOnUiThread methods for sending data to server

for Implement AsyncTask

Link1 Link2 Link3 Link4

Hope it will help you.

Community
  • 1
  • 1
Sanket Shah
  • 4,352
  • 3
  • 21
  • 41
0

Can you paste the exception you are getting from ddms-log when force close happens. That way we can get the exact idea why this is happening. From what i am guessing you are probably doing network operation in Main Thread(UI) which is not allowed after Android 3.0. Please check out this post for details. I might be wrong about that since i am assuming.

AsyncTask:

Thread :

new Thread(){
  public void run(){
    HttpEntity entity;
    HttpClient client = new DefaultHttpClient();
    String url ="http://67.23.166.35:80/android/insertv2.php";

    HttpPost request = new HttpPost(url);
    StringEntity se = new StringEntity(datatoServer);
    se.setContentEncoding("UTF-8");
    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
    entity = se;
    request.setEntity(entity);
    HttpResponse response = client.execute(request);
    entity = response.getEntity();
        runOnUiThread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                // do what you have to do
            }
    });
  }
}.start();
Community
  • 1
  • 1
saiful103a
  • 1,109
  • 7
  • 13
  • There is 2 ways to solve this as i can think of. Use AsyncTask as everyone is suggesting or you can just use old fashioned thread. Put the above code in an asynctask or in a thread. Both always worked for me. – saiful103a Nov 14 '13 at 07:05
  • Edited my answer for your convenience. :) – saiful103a Nov 14 '13 at 07:14
  • First thread is going to be our thread for network call, and the second thread[since we are calling it via runOnUiThread] it is going to attach itself to the Main UI thread bcz we need our response to show in UI thread. [runOnUiThread](http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)) here is a bit detail explanation. – saiful103a Nov 14 '13 at 07:26
  • if I use a synchronous task should I start to things at a time. My service and a thread. Service for all functionality like calculate and store data. and thread to fetch and send data – user2686960 Nov 14 '13 at 07:27
  • First of all since you are already using service there was no need for thread/asyntask in that matter. You could've used broadcastreceiver to pass data between you UI and the service. I use that all the time. – saiful103a Nov 14 '13 at 07:32
  • You are doing a network operation in the thread so definitely it will have some response. I put it runOnUiThread there so that if you were to use that response in you main UI thread you can do that in runOnUiThread. If not you can just delete that portion. – saiful103a Nov 14 '13 at 07:34
0

Donot do network operations in main thread.Use AyncTask

Example:

 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

    @Override
    protected void onPreExecute() {
         //Show UI

    }

    @Override
    protected Void doInBackground(Void... arg0) {
         // do your background process 
        HttpEntity entity;
        HttpClient client = new DefaultHttpClient();
        String url ="http://67.23.166.35:80/android/insertv2.php";

        HttpPost request = new HttpPost(url);
        StringEntity se = new StringEntity(datatoServer);
        se.setContentEncoding("UTF-8");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
        entity = se;
        request.setEntity(entity);
        HttpResponse response = client.execute(request);
        entity = response.getEntity();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
                      //Show UI (Toast msg here)

    }

    };

    task.execute((Void[])null);

else you can add

 super.onCreate(savedInstanceState);

         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
Kalai.G
  • 1,610
  • 13
  • 22