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);