• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps JSON Error

ac4android

Well-Known Member
I am trying to access a remote MySQL server and got this JSON error, actually it doesn't seem related to JSON because the debugger says it is an "unexpected status line"... I can't quite put my finger on it, been working whole day :(

Code:
 @Override
  protected String doInBackground(String... arg0) {
  String projectName = arg0[0];
  String driverLicense = arg0[1];
  String licensePlate = arg0[2];

  String link;
  String data;
  BufferedReader bufferedReader;
  String result;

  try {
  data = "?projectname=" + URLEncoder.encode(projectName, "UTF-8");
  data += "&driverlicense=" + URLEncoder.encode(driverLicense, "UTF-8");
  data += "&licenseplate=" + URLEncoder.encode(licensePlate, "UTF-8");

  link = "http://<ip-address>:<port>/sub-directory /ConAndroidMySQL.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) {
  String jsonStr = result;
  if (jsonStr != null) {
  try {
  JSONObject jsonObj = new JSONObject(jsonStr);
  String query_result = jsonObj.getString("query_result");
  if (query_result.equals("SUCCESS")) {
  Toast.makeText(context, "Data inserted successfully. Signup successfull.", Toast.LENGTH_SHORT).show();
  } 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();
  }
  }
JSONError.JPG
 
I am trying to access a remote MySQL server and got this JSON error, actually it doesn't seem related to JSON because the debugger says it is an "unexpected status line"... I can't quite put my finger on it, been working whole day :(

Code:
@Override
  protected String doInBackground(String... arg0) {
  String projectName = arg0[0];
  String driverLicense = arg0[1];
  String licensePlate = arg0[2];

  String link;
  String data;
  BufferedReader bufferedReader;
  String result;

  try {
  data = "?projectname=" + URLEncoder.encode(projectName, "UTF-8");
  data += "&driverlicense=" + URLEncoder.encode(driverLicense, "UTF-8");
  data += "&licenseplate=" + URLEncoder.encode(licensePlate, "UTF-8");

  link = "http://<ip-address>:<port>/sub-directory /ConAndroidMySQL.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) {
  String jsonStr = result;
  if (jsonStr != null) {
  try {
  JSONObject jsonObj = new JSONObject(jsonStr);
  String query_result = jsonObj.getString("query_result");
  if (query_result.equals("SUCCESS")) {
  Toast.makeText(context, "Data inserted successfully. Signup successfull.", Toast.LENGTH_SHORT).show();
  } 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();
  }
  }
View attachment 105308
Here it is again...

JSONError.JPG
 
Oh hi, u r back!
The string that came back had some spurious characters prepended to it, even though in the .PHP on the server side, I inserted content type instruction, like this:
Code:
<?php

header('Content-Type: application/json');
$con=mysqli_connect("localhost", "scott", "tiger", "testDB");

if (mysqli_connect_errno($con))
{
  echo "Failed to connect to MySQL: ".mysqli_connect_error();
}
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysqli_query($con,"SELECT Role FROM table123 where
Username='$username' and Password='$password'");
$row = mysqli_fetch_array($result);
$data = $row[0];

if($data){
echo $data;
}
mysqli_close($con);
?>

I do not want to use trickery, like sub-stringing into the string until I find the first "{" and pass the rest from "{" onward to JSON. I like to know why there are characters coming back from the query on the server.
 
Here try something like this. I use this to perform a get operation from a server so I should be ok with a few tweaks here and there
Java:
private String httpDownloadData(String myUrl)

    {
        String respone = null;
        HttpURLConnection urlConnection = null;
        try
        {
            URL url = new URL(myUrl);
            //put in the username and pssword for parmas to send to url
            //this is good for login
            if (username!=null)
            {
                Authenticator.setDefault(new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password.toCharArray());
                    }
                });
            }

            urlConnection = (HttpURLConnection)url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();

            if(inputStream != null)
            {
                respone = streamToString(inputStream);
                inputStream.close();
            }

        }catch (IOException ie)
        {
            //ie.printStackTrace();
            Log.d("Issue with Http: " , "IOException");
            ie.printStackTrace();

        }finally {
            if(urlConnection != null)
            {
                urlConnection.disconnect();
            }
        }
        return respone;
    }
 
Back
Top Bottom