Se connecter a une base de donnée externe

Fermé
theclemmm Messages postés 71 Date d'inscription mardi 16 octobre 2012 Statut Membre Dernière intervention 29 septembre 2016 - 24 avril 2015 à 20:51
BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 - 25 avril 2015 à 12:17
Bonjour, je n'arrive pas à me connecter a ma base de donnée externe malgré tous les tutos existants. Voici mon code:

package com.xolider.tof;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;


public class MainActivity extends ActionBarActivity implements View.OnClickListener {

EditText editText;
EditText pass;
Button button;
RelativeLayout layout;
TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = (RelativeLayout)RelativeLayout.inflate(this, R.layout.activity_main, null);
editText = (EditText)layout.findViewById(R.id.editText);
editText.setHint("email");
pass = (EditText)layout.findViewById(R.id.editText2);
pass.setHint("password");
button = (Button)layout.findViewById(R.id.button);
button.setOnClickListener(this);
text = (TextView)layout.findViewById(R.id.textView);
setContentView(layout);
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
String myurl = "http://192.168.1.199/android/connect.php";
InputStream is;
String result;
String re = null;
try {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(myurl);
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
JSONArray array = new JSONArray(result);
for(int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
if(object.getString("email").equals(editText.getText().toString())) {
if(object.getString("pass").equals(pass.getText().toString())) {
Intent intent = new Intent(MainActivity.this, MainActivity2Activity.class);
startActivity(intent);
this.finish();
}
else {
Log.e("erreur pass", "erreur de password");
text.setText("erreur pass");
}
}
else {
Log.e("erreur email", "erreur email");
text.setText("erreur email");
}
}
}
catch (Exception e) {
Log.i("Erreur !", e.getStackTrace().toString());
}
break;
}
}
}


Pouvez-vous m'aider ou me proposer une autre solution svp ? Merci d'avance



A voir également:

1 réponse

BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 3 894
25 avril 2015 à 12:17
Hello,

Quand tu dis "ça marche pas", peux-tu être plus précis? As-tu testé en debug?

Tu appelles son webservice "connect". Ok. Mais la vérification login/password doit être faite côté serveur et non côté appli ;) CE webservice doit te retourner une valeur OK/KO ou bien un jeton d'authentification...
0