-
-
Save rrymm/2a08691c97f9b13d6eff2d878599f3b0 to your computer and use it in GitHub Desktop.
this is a login in android using HTTP,POST,JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.situc; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.http.Header; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.apache.http.util.EntityUtils; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.app.ListActivity; | |
import android.content.Intent; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
public class Login extends Activity { | |
private EditText txt_username,txt_password; | |
private Button btn_login; | |
private String username,password; | |
private String result; | |
Login_controller login_controller = new Login_controller(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
txt_username = (EditText)findViewById(R.id.txtUsername); | |
txt_password = (EditText)findViewById(R.id.txtPassword); | |
btn_login = (Button)findViewById(R.id.btnLogin); | |
btn_login.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String[] data = new String[2]; | |
data[0] = txt_username.getText().toString(); | |
data[1] = txt_password.getText().toString(); | |
result = login_controller.post("http://10.0.2.2:80/SITUC/index.php/welcome/login",data); | |
try | |
{ | |
if(result != null) | |
{ | |
JSONArray js = new JSONArray(result); | |
if(js.length() > 0) | |
{ | |
Log.i("lenght",String.valueOf(js.length())); | |
String name = ""; | |
Intent statrWelcomeLayout = new Intent(Login.this,Welcome.class); | |
for(int i=0;i<js.length();i++) | |
{ | |
JSONObject item = js.getJSONObject(i); | |
name = item.getString("nombre"); | |
} | |
statrWelcomeLayout.putExtra("nombre", name); | |
startActivity(statrWelcomeLayout); | |
} | |
else | |
{ | |
Log.i("exepcion","no encontro"); | |
} | |
} | |
else | |
{ | |
Log.i("status","error"); | |
} | |
} catch (JSONException e) | |
{ | |
Log.i("exepcion","lanzada"); | |
} | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.login, menu); | |
return true; | |
} | |
@Override | |
protected void onPause() { | |
// TODO Auto-generated method stub | |
super.onPause(); | |
finish(); | |
} | |
} | |
/*HTTP HANDLER*/ | |
package com.example.situc; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpException; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.apache.http.util.EntityUtils; | |
import android.util.Log; | |
public class Login_controller | |
{ | |
public String post(String url,String[] data) | |
{ | |
String textResult = null; | |
try | |
{ | |
HttpClient httpclient = new DefaultHttpClient(); //allows do the http petition | |
HttpPost post = new HttpPost(url); | |
List<NameValuePair> itemsPost = new ArrayList<NameValuePair>(); | |
itemsPost.add(new BasicNameValuePair("username", data[0])); | |
itemsPost.add(new BasicNameValuePair("password", data[1])); | |
post.setEntity(new UrlEncodedFormEntity(itemsPost)); | |
HttpResponse respuesta = httpclient.execute(post); | |
HttpEntity ent = respuesta.getEntity();/*y obtenemos una respuesta*/ | |
textResult = EntityUtils.toString(ent); | |
} | |
catch(Exception e) | |
{ | |
} | |
return textResult; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment