Luis_Augusto_Santos 10 de abr. de 2017
Certamente o problema está no código que você utiliza no android.
Pode postá-lo por completo?
Felipe_Botelho 10 de abr. de 2017
Luis primeiramente obrigado pela ajuda já estava perdido aqui.
public void onClick ( View v ) {
if ( v . getId () == R . id . btnEntrar ){
RA = AlunoRA . getText (). toString ();
Senha = AlunoSenha . getText (). toString ();
final JSONObject obj = new JSONObject ();
try {
obj . put ( "RA" , RA );
obj . put ( "Pass" , Senha );
} catch ( JSONException e ) {
e . printStackTrace ();
}
this . runOnUiThread ( new Runnable () {
@Override
public void run () {
String caminho = "http://10.0.2.2/system/pages/login" ;
HttpURLConnection conn = null ;
BufferedReader reader = null ;
try {
URL urlConection = new URL ( caminho );
conn = ( HttpURLConnection ) urlConection . openConnection ();
conn . setDoOutput ( true );
conn . setRequestMethod ( "POST" );
PrintStream ps = new PrintStream ( conn . getOutputStream ());
ps . printf ( "json" , obj . toString ());
conn . connect ();
String resposta = new Scanner ( conn . getInputStream ()). next ();
/*JSONArray arr = new JSONArray(new Scanner(conn.getInputStream()).next());
JSONObject resp = new JSONObject(resposta);
*/
InputStream in = new BufferedInputStream ( conn . getInputStream ());
reader = new BufferedReader ( new InputStreamReader ( in ));
String line = "" ;
StringBuffer buffer = new StringBuffer ();
while (( line = reader . readLine ()) != null ){
buffer . append ( line );
}
JSONArray jresp = null ;
jresp = new JSONArray ( buffer . toString ());
//JSONObject jo = new JSONObject(jresp.toString());
Log . d ( "JSON" , obj . toString ());
Log . d ( "resposta" , buffer . toString ());
/*
if(resposta.equals("1")){
Intent abreHome = new Intent(Login.this, Home.class);
startActivity(abreHome);
} else {
Toast.makeText(Login.this, "Ops houve um erro. Tente novamente", Toast.LENGTH_SHORT).show();
// Log.d("retorno", resp.toString());
}*/
} catch ( MalformedURLException e ) {
e . printStackTrace ();
} catch ( IOException e ) {
e . printStackTrace ();
} catch ( JSONException e ) {
e . printStackTrace ();
}
}
});
}
Esse código acima corresponde a todo o meu onClick.
Quanto a esse é referente de toda a classe
package com.example.felipe.tutor ;
import android.content.Intent ;
import android.net.http.HttpResponseCache ;
import android.support.v7.app.AppCompatActivity ;
import android.os.Bundle ;
import android.util.Log ;
import android.util.PrintStreamPrinter ;
import android.view.View ;
import android.widget.EditText ;
import android.widget.ImageButton ;
import android.widget.Toast ;
import org.json.JSONArray ;
import org.json.JSONException ;
import org.json.JSONObject ;
import java.io.BufferedInputStream ;
import java.io.BufferedReader ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.InputStreamReader ;
import java.io.PrintStream ;
import java.net.HttpURLConnection ;
import java.net.MalformedURLException ;
import java.net.URL ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Scanner ;
public class Login extends AppCompatActivity implements View . OnClickListener {
ImageButton entrar ;
EditText AlunoRA , AlunoSenha ;
String RA , Senha ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_login );
/* getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
*/
AlunoRA = ( EditText ) findViewById ( R . id . edtRA );
AlunoSenha = ( EditText ) findViewById ( R . id . edtPass );
entrar = ( ImageButton ) findViewById ( R . id . btnEntrar );
entrar . setOnClickListener ( this );
}
@Override
public void onClick ( View v ) {
if ( v . getId () == R . id . btnEntrar ){
RA = AlunoRA . getText (). toString ();
Senha = AlunoSenha . getText (). toString ();
final JSONObject obj = new JSONObject ();
try {
obj . put ( "RA" , RA );
obj . put ( "Pass" , Senha );
} catch ( JSONException e ) {
e . printStackTrace ();
}
this . runOnUiThread ( new Runnable () {
@Override
public void run () {
String caminho = "http://10.0.2.2/system/pages/login" ;
HttpURLConnection conn = null ;
BufferedReader reader = null ;
try {
URL urlConection = new URL ( caminho );
conn = ( HttpURLConnection ) urlConection . openConnection ();
conn . setDoOutput ( true );
conn . setRequestMethod ( "POST" );
PrintStream ps = new PrintStream ( conn . getOutputStream ());
ps . printf ( "json" , obj . toString ());
conn . connect ();
String resposta = new Scanner ( conn . getInputStream ()). next ();
/*JSONArray arr = new JSONArray(new canner(conn.getInputStream()).next());
JSONObject resp = new JSONObject(resposta);
*/
InputStream in = new BufferedInputStream ( conn . getInputStream ());
reader = new BufferedReader ( new InputStreamReader ( in ));
String line = "" ;
StringBuffer buffer = new StringBuffer ();
while (( line = reader . readLine ()) != null ){
buffer . append ( line );
}
JSONArray jresp = null ;
jresp = new JSONArray ( buffer . toString ());
//JSONObject jo = new JSONObject(jresp.toString());
Log . d ( "JSON" , obj . toString ());
Log . d ( "resposta" , buffer . toString ());
/*
if(resposta.equals("1")){
Intent abreHome = new Intent(Login.this, Home.class);
startActivity(abreHome);
} else {
Toast.makeText(Login.this, "Ops houve um erro. Tente novamente", Toast.LENGTH_SHORT).show();
// Log.d("retorno", resp.toString());
}*/
} catch ( MalformedURLException e ) {
e . printStackTrace ();
} catch ( IOException e ) {
e . printStackTrace ();
} catch ( JSONException e ) {
e . printStackTrace ();
}
}
});
}
}
}
Luis_Augusto_Santos 10 de abr. de 2017
Você está requisitando um WS rest?
Se for, dá uma olhada nisso