1
Hello, I am trying to get the data of a query via HTTP, but it is returning me this error:
org.json.JSONException: End of input at character 0 of
The Code I have is this:
private class AsyncFetch extends AsyncTask<String, String, String> {
ProgressDialog pdLoading = new ProgressDialog(BuscaCAActivity.this);
HttpURLConnection conn;
URL url = null;
String searchQuery;
public AsyncFetch(String searchQuery){
this.searchQuery=searchQuery;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("\tFazendo a busca...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
try {
// Enter URL address where your php file resides
url = new URL(minha_url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
// setDoInput and setDoOutput to true as we send and recieve data
conn.setDoInput(true);
conn.setDoOutput(true);
// add parameter to our above url
Uri.Builder builder = new Uri.Builder().appendQueryParameter("codigo", searchQuery);
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return("Connection error");
}
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
pdLoading.dismiss();
if(result.equals("no result")) {
Toast.makeText(BuscaCAActivity.this, getString(R.string.nenhum_resultado), Toast.LENGTH_LONG).show();
}else{
try {
JSONArray jArray = new JSONArray(result);
// Extract data from json and store into ArrayList as class objects
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
tvTeste.setText(json_data.getString("NRRegistro"));
tvTeste2.setText(json_data.getString("DataValidade"));
tvTeste3.setText(json_data.getString("Situacao"));
tvTeste4.setText(json_data.getString("NRProcesso"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
PHP looks like this:
if(isset($_POST['codigo']))
{
require_once('config.inc.php');
$search_query=$_POST['codigo'];
$sql = 'SELECT NRRegistro, DataValidade, Situacao, NRProcesso FROM tab_cadastro WHERE NRRegistro = :search_query';
$statement = $connection->prepare($sql);
$statement->bindParam(':search_query', $search_query, PDO::PARAM_STR);
$statement->execute();
if($statement->rowCount())
{
$row_all = $statement->fetchall(PDO::FETCH_ASSOC);
header('Content-type: application/json');
echo json_encode($row_all);
}
elseif(!$statement->rowCount())
{
echo "no result";
}
}
I’ve been trying for hours to find the mistake and I couldn’t move forward. What’s wrong with those codes?
in theInBackground after the whole result process has value?
– Edson Reis
@Edsonreis, in the connection part is all ok. The problem is in reading the Json on the server. I’ve tested the sql statement in phpmyadmin and it’s right, so I don’t know where the error is.
– Henqsan
read the json inside theInBackground
– Edson Reis
You are giving "POST" and you are waiting for a result to assemble your object... it would not be "GET"?
– Edson Reis
I’ve tried with Get too and nothing. Coding, type of field in the database and table influences something?
– Henqsan
The error that is happening says that no data is coming to mount your array, its result is 0 when it mounts in onPostExecute().
– Edson Reis
I’ll post an answer and you tell me if it worked.
– Edson Reis
I posted an answer, to move to view Voce will have to create an Adapter passing your list to the view
– Edson Reis