Pregunta

I have a problem with Socket connection from Android device to pc.
in first time, in LoginActivity, my app connect correctly using a login and password to the server with socket.
but, in second time, in CompteActivity, when I want connect to import some data from data base with same socket, my app was stopped.
But, in emulator is run without problem.

In logcat:

04-21 06:45:26.639: E/AndroidRuntime(13862): FATAL EXCEPTION: main
04-21 06:45:26.639: E/AndroidRuntime(13862): android.os.NetworkOnMainThreadException
04-21 06:45:26.639: E/AndroidRuntime(13862):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at libcore.io.IoBridge.connect(IoBridge.java:112)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at java.net.Socket.startupSocket(Socket.java:566)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at java.net.Socket.tryAllAddresses(Socket.java:127)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at java.net.Socket.<init>(Socket.java:177)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at java.net.Socket.<init>(Socket.java:149)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at pfe.mobilebanking.atbmobile.CompteActivity$1.onClick(CompteActivity.java:81)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at android.view.View.performClick(View.java:3516)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at android.view.View$PerformClick.run(View.java:14110)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at android.os.Handler.handleCallback(Handler.java:605)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at android.os.Looper.loop(Looper.java:137)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at  android.app.ActivityThread.main(ActivityThread.java:4429)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at java.lang.reflect.Method.invokeNative(Native Method)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at java.lang.reflect.Method.invoke(Method.java:511)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-21 06:45:26.639: E/AndroidRuntime(13862):    at dalvik.system.NativeStart.main(Native Method)
¿Fue útil?

Solución

Without code I can't help you much, but this Exception should point you in the right direction:

04-21 06:45:26.639: E/AndroidRuntime(13862): android.os.NetworkOnMainThreadException

Basically in newer OS versions (I think 4.0+) Google turned on StrictMode which does not allow any access to the network on the main UI thread. They did this specifically to help developers since the Network can take an indeterminate amount of time and that will stop the UI from being responsive or updating while the network call is ongoing.

So, you need to find the code that is importing the data from the socket and put that into an AsyncTask, and IntentService or some other off Main UI thread.

Otros consejos

Ok! in manifest:

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="18" />

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

this is my code in LoginActivity and CompteActitvity...

LoginActivity:

public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

    String cin = null;
    String pass = null;
    String reponse = null;

    public UserLoginTask(String cin, String pass) {
        // TODO Auto-generated constructor stub
        this.cin = cin;
        this.pass = pass;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        // TODO: attempt authentication against a network service.

        try {
            // Simulate network access.
            Thread.sleep(2000);

        } catch (InterruptedException e) {
            return false;
        }


        try {
            clientSocket = new Socket(Inet4Address.getByName("192.168.100.1"), 2014);
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            out = new PrintWriter(clientSocket.getOutputStream());
            out.println("connexion");
            out.println(cin);
            out.println(pass);
            out.flush();
            reponse = in.readLine();

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

        if(reponse.equals("ok"))
            return true;
        else
            return false;

        // TODO: register the new account here.
        //return true;
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        mAuthTask = null;
        showProgress(false);

        if (success) {
            finish();

            String [] numCompte = new String [10];
            int i=0;
            try {
                System.out.println("envoie de INtent");
                while(in.ready())
                {
                    numCompte [i] = in.readLine();
                    System.out.println(numCompte[i]);
                    i++;
                }
                clientSocket.close();
                in.close();
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Intent secondeActivity = new Intent(LoginActivity.this,        EspaceClientActivity.class);

            secondeActivity.putExtra("numero_compte", numCompte);

            startActivity(secondeActivity);

        } else {
            mPasswordView
                    .setError(getString(R.string.error_incorrect_password));
            mPasswordView.requestFocus();
        }
    }

CompteActivity:

OnClickListener afficher = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        int select = 0;
        float total = 0;

        try {
            System.out.println("lancement de socket");
            Socket clientSocket  = new Socket(Inet4Address.getByName("192.168.100.1"), 2014);
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            out = new PrintWriter(clientSocket.getOutputStream());
            out.println("compte");

            String [] compteSelected = new String[mNumCompte.length];
            for(int i = 0;i<mListCompte.getCount();i++)
            {
                if(mListCompte.isItemChecked(i))
                {
                    compteSelected [select] = mListCompte.getItemAtPosition(i).toString();
                    //System.out.println(compteSelected[select]);
                    out.println(compteSelected[select]);
                    select++;
                }
            }

            out.flush();

            mSoldeCompte = new String[select+1];

            for (int i = 0; i<select;i++)
            {
                reponse = in.readLine();
                mSoldeCompte[i]=reponse;
                //System.out.println(mSoldeCompte[i]);
                total += Float.parseFloat(mSoldeCompte[i]);
            }
            //System.out.println("Float à ajouter..." + total);
            mSoldeCompte[select]= String.valueOf(total).toString();
            //System.out.println("valeur ajoutée..."+mSoldeCompte[select]);
            String [][] afficheElementSolde = new String[select+1][select+1];
            for(int i = 0;i<select;i++)
            {
                afficheElementSolde[i][0] = compteSelected[i];
            }
            for(int i = 0;i<select+1;i++)
            {
                afficheElementSolde[i][1] = mSoldeCompte[i];
            }
            afficheElementSolde[select][0]="Total";


            List<HashMap<String, String>> liste = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> element;

            for(int i = 0;i<select+1;i++)
            {
                element = new HashMap<String, String>();
                element.put("num_compte", afficheElementSolde[i][0]);
                element.put("sol_compte", afficheElementSolde[i][1]);
                liste.add(element);
            }

            //System.out.println("affichage de solde");
            ListAdapter adapter = new SimpleAdapter(CompteActivity.this , liste, android.R.layout.simple_list_item_2, new String[] {"num_compte","sol_compte"}, new int[]{android.R.id.text1,android.R.id.text2});
            mListSolde.setAdapter(adapter);



            clientSocket.close();
            in.close();
            out.close();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

};

Just for remind you, this code run without problem in emulator, and in android device (os version: 4.0.4) the problem is located in CompteActivity at this line:

Socket clientSocket  = new Socket(Inet4Address.getByName("192.168.100.1"), 2014);

Good evening thank you man for help! :) with your remark about a version of OS 4.0+ and I put my code into class extends AsyncTask in the CompteActivity, like you told me...

this is my new code runnable correctly:

public class CompteActivity extends Activity {

private UserCompteTask mSoldeTask;
private BufferedReader in = null;
private PrintWriter out = null;
String reponse = null;
Socket clientSocket;

private ListView mListCompte = null;
private ListView mListSolde = null;
private String [] mNumCompte = null;

//private String [] mSoldeCompte = null;

private Button boutonAfficher = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compte);

    mListCompte = (ListView)findViewById(R.id.list_num_compte_compte);
    mListSolde = (ListView)findViewById(R.id.info_compte);

    boutonAfficher = (Button)findViewById(R.id.button_compte_affiche);


    boutonAfficher.setOnClickListener(afficher);


    Intent chargeNum = getIntent();
    mNumCompte = chargeNum.getStringArrayExtra("liste_compte");

    mListCompte.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, mNumCompte));
    mListCompte.setItemChecked( 0, false);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.compte, menu);
    return true;
}

OnClickListener afficher = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        mSoldeTask = new UserCompteTask();
        mSoldeTask.execute((Void) null);


    }

};

public class UserCompteTask extends AsyncTask<Void, Void, Boolean>
{
    int select = 0;
    float total = 0;
    private String [] mSoldeCompte = null;
    String [] compteSelected = new String[mNumCompte.length];

    @Override
    protected Boolean doInBackground(Void... params) {
        // TODO Auto-generated method stub

        try {
            // Simulate network access.
            Thread.sleep(1000);

        } catch (InterruptedException e) {
            return false;
        }



        try
        {

            clientSocket  = new Socket("192.168.100.1", 2014);
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            out = new PrintWriter(clientSocket.getOutputStream());
            out.println("compte");

            for(int i = 0;i<mListCompte.getCount();i++)
            {
                if(mListCompte.isItemChecked(i))
                {
                    compteSelected [select] = mListCompte.getItemAtPosition(i).toString();
                    //System.out.println(compteSelected[select]);
                    out.println(compteSelected[select]);
                    select++;
                }
            }

            out.flush();

            mSoldeCompte = new String[select+1];

            for (int i = 0; i<select;i++)
            {
                reponse = in.readLine();
                mSoldeCompte[i]=reponse;
                //System.out.println(mSoldeCompte[i]);
                total += Float.parseFloat(mSoldeCompte[i]);
            }
            //System.out.println("Float à ajouter..." + total);
            mSoldeCompte[select]= String.valueOf(total).toString();
            //System.out.println("valeur ajoutée..."+mSoldeCompte[select]);

            clientSocket.close();
            in.close();
            out.close();
        }catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return true;
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        mSoldeTask = null;

        if (success) {

            String [][] afficheElementSolde = new String[select+1][select+1];
            for(int i = 0;i<select;i++)
            {
                afficheElementSolde[i][0] = compteSelected[i];
            }
            for(int i = 0;i<select+1;i++)
            {
                afficheElementSolde[i][1] = mSoldeCompte[i];
            }
            afficheElementSolde[select][0]="Total";

            List<HashMap<String, String>> liste = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> element;

            for(int i = 0;i<select+1;i++)
            {
                element = new HashMap<String, String>();
                element.put("num_compte", afficheElementSolde[i][0]);
                element.put("sol_compte", afficheElementSolde[i][1]);
                liste.add(element);
            }

            //System.out.println("affichage de solde");
            ListAdapter adapter = new SimpleAdapter(CompteActivity.this , liste, android.R.layout.simple_list_item_2, new String[] {"num_compte","sol_compte"}, new int[]{android.R.id.text1,android.R.id.text2});
            mListSolde.setAdapter(adapter);
        }
    }

    @Override
    protected void onCancelled() {
        mSoldeTask = null;

    }
}

}

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top