1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
package com.example.myapp;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class connexion extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connexion);
}
public void Seconnecter(View v) {
if (reseauOK()) {
new JSONParse().execute();
} else {
Context context = getApplicationContext();
CharSequence text = "Reseau mobile non disponible ";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
private boolean reseauOK(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting())
return true;
else
return false;
}
public class JSONParse extends AsyncTask<Void,Void,Void> {
Client client = null;
private boolean erreurWS;
heure leParent;
TextView f6m;
String mdpC;
String nomC;
private ProgressDialog pDialog;
TextView pw;
public JSONParse() {
this.f6m = (TextView) connexion.this.findViewById(R.id.mail);
this.nomC = this.f6m.getText().toString();
this.pw = (TextView) connexion.this.findViewById(R.id.mdp);
this.mdpC = this.pw.getText().toString();
this.erreurWS = false;
this.client = null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
this.pDialog = new ProgressDialog(connexion.this);
this.pDialog.setMessage("Veuillez patienter :) ");
this.pDialog.setIndeterminate(false);
this.pDialog.setCancelable(true);
this.pDialog.show();
}
@Override
protected Void doInBackground(Void... args) {
JSONObject jsonProduits = lireJson();
if (!this.erreurWS) {
jsonToTextView(jsonProduits);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
this.pDialog.dismiss();
if (this.erreurWS) {
Context context = getApplicationContext();
CharSequence text = "UNE ERR PRODUITE ";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return;
}
if(this.client == null){
Context context = getApplicationContext();
CharSequence text = "Ca marche pas ";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}else{
Context context = getApplicationContext();
CharSequence text = "Ca marche ";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent intent = new Intent(connexion.this, MyActivityClient.class);
intent.putExtra("idC", this.client.getIdC());
startActivity(intent);
finish();
}
}
private JSONObject lireJson(){
String myurl= "http://www.siomende.fr/anoll/Vala/ws/ws.php?action=seconnecter&nomC=" + this.nomC + "&mdpC=" + this.mdpC;
InputStream is = null;
String result = "";
JSONObject jArray = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(myurl);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
jArray = new JSONObject(result);
}catch(Exception e){
erreurWS=true ;
}
return jArray;
}
private void jsonToTextView(JSONObject json) {
JSONObject jsonObject = json;
try {
if (jsonObject.getString("nomC") == null) {
Context context = getApplicationContext();
CharSequence text = "id/mdp érronée ";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} else {
this.client = new Client(jsonObject.getLong("idC"), jsonObject.getString("nomC"), jsonObject.getString("prenomC"), jsonObject.getString("adresseC"), jsonObject.getString("telephoneC"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} |
Partager