Ao passar os dados no navegador desta forma:
http://127.0.0.1/system/pages/login?json={%22Pass%22:%22123456%22,%22RA%22:%22242252014%22}.json Obtenho o seguinte retorno: {"User":{"id":"1","status_id":"1","name":"Felipe","RA":"242252014","email":"teste@gmail.com","password":"123456","data_cancel":null},"Status":{"id":"1","name":"Ativo"}}
Código Cakephp:
public function login(){
$this->autoRender = false;
$this->response->type('application/json');
$this->layout = null;
$dados = json_decode(str_replace(".json", "", $_REQUEST["json"]), true);
$ra = $dados["RA"];
$senha = $dados["Pass"];
$usuario = $this->User->find("first",
["conditions" => ["User.RA" => $ra, "User.password" => $senha ] ]);
$retorno = array($usuario);
header('Content-type: application/json');
echo json_encode($retorno);
}
Pego os dados da seguinte forma no android:
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());
Como resolver?
Certamente o problema está no código que você utiliza no android.
Pode postá-lo por completo?
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();
}
}
});
}
}
}
Você está requisitando um WS rest?
Se for, dá uma olhada nisso