Estou a tentar desenvolver um programa em Android para a escola, mas não tenho experiência nenhuma.
Está-me a dar o erro de parsing JSON data.
Aqui está o código php:
<?php $con=mysqli_connect("localhost","prof","dbesms","prr"); if (mysqli_connect_errno($con)) { echo '{"query_result":"ERROR"}'; } $userName = $_GET['username']; $passWord = $_GET['password']; $result = mysqli_query($con,"SELECT * FROM login WHERE User='$userName' AND Pass='$passWord'"); $numRegistos=mysqli_num_rows($result); if($result == true && $numRegistos>0) { echo '{"query_result":"SUCCESS","numMesas":"6"}'; } else{ echo '{"query_result":"FAILURE"}'; } mysqli_close($con); ?>Agora o código Android:
package com.example.toshiba.prr;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class Registo extends AsyncTask<String, Void, String> {
private Context context;
public Registo(Context context) {
this.context = context;
}
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0) {
String username = arg0[0];
String password = arg0[1];
String link;
String data;
BufferedReader bufferedReader;
String result;
try {
data = "?username=" + URLEncoder.encode(username, "UTF-8");
data += "&password=" + URLEncoder.encode(password, "UTF-8");
link = "http://10.0.2.2/PRR/android/check.php"+data;
URL url = new URL(link);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
result = bufferedReader.readLine();
return result;
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(context, "Resultado."+result, Toast.LENGTH_SHORT).show();
String jsonStr = result;
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String query_result = jsonObj.getString("query_result");
if (query_result.equals("SUCCESS")) {
String nMesas=jsonObj.getString("numMesas");
Toast.makeText(context, "Data inserted successfully. Signup successful."+nMesas, Toast.LENGTH_SHORT).show();
// chama proximo ecra enviando num Mesas.
entraServico(nMesas);
} else if (query_result.equals("FAILURE")) {
Toast.makeText(context, "Data could not be inserted. Signup failed.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Couldn't connect to remote database.", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show();
}
}
private void entraServico(String str) {
Context esteContexto=this.context; // notar que context foi fefinido no topo da classe e portanto esta disponivel como variavel global;
Intent i = new Intent(esteContexto,Tipo.class);
Bundle bundle = new Bundle();
bundle.putString("numMesa", str);
i.putExtras(bundle);
context.startActivity(i);
}
}
Agradecia desde já toda a ajuda.