0

I know that this Question is repeated but I can't find the answer in Internet.

02-10 11:03:28.465 22410-22767/com.eztimetable.eztimetable I/System.out﹕ ERROR : null 02-10 11:03:28.465 22410-22767/com.eztimetable.eztimetable I/System.out﹕ error 02-10 11:03:28.595 22410-22410/com.eztimetable.eztimetable E/log_tag﹕ Error parsing data org.json.JSONException: Value error of type java.lang.String cannot be converted to JSONArray

why am i getting error / null ?

Below is the method that i use

//in LecturerScreen class
    public String getDataFromDB() {
    try {
        httpclient = new DefaultHttpClient();
        httppost= new HttpPost("http://192.168.1.9/my_folder_inside_htdocs/retrieve.php"); // make sure the url is correct.
        //add your data
        nameValuePairs = new ArrayList<NameValuePair>(2);
        // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
        nameValuePairs.add(new BasicNameValuePair("inputUser",inputUser.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost,responseHandler);

        return response.trim();

    } catch (Exception e) {
        System.out.println("ERROR : " + e.getMessage());
        return "error";
    }
}

and this is how i call the method

    //in LecturerTimetable class
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lecturer_timetable);

    tl = (TableLayout) findViewById(R.id.maintable);

    final LecturerScreen getdb = new LecturerScreen();

    new Thread(new Runnable() {
        public void run() {
            data = getdb.getDataFromDB();
            System.out.println(data);

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    ArrayList<TimetableDisplay> timetable = parseJSON(data);
                    addData(timetable);
                }
            });

        }
    }).start();
}

I dont know what where my wrong here. Could someone give me solution for this question.

I am very new in developing Android program, so if anyone could guide, it would be an honor. Sorry for bad english. Thank you.

--UPDATE--

the php script

<?php
$hostname_localhost ="localhost";
$database_localhost ="eztimetable";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);

$lecturerUser = $_POST['inputUser'];
$query_search = "select lecturerCode from lectureraccount where lecturerId = '".$lecturerUser."'";
$query_exec = mysql_query($query_search,$localhost) or die(mysql_error());
while($row=mysql_fetch_assoc($query_exec))
        $json_output[]=$row;

print(json_encode($json_output));

mysql_close();   

?>

9
  • You're not returning anything when your try block succeeds. Commented Feb 10, 2015 at 3:25
  • Hi prudhvi , what should i do ? can you give some solution? sorry , i am very new to programming.. Commented Feb 10, 2015 at 3:27
  • My bad, I see you're returning 'response.trim()'. But check if your response is referring to null. Add 'Log.w("String", response);' just below final String response = httpclient.execute(httppost,responseHandler); and paste the Log message you get. Commented Feb 10, 2015 at 3:30
  • I add the line and it gives the same Log message as in the question. Commented Feb 10, 2015 at 3:47
  • @SkyClasher What kind of response are you expecting from the URL? Commented Feb 10, 2015 at 4:24

2 Answers 2

1

You are seeing this error (Error parsing data org.json.JSONException: Value error of type java.lang.String cannot be converted to JSONArray) because your response is a JSONArray and you are treating it as a String. In order to get rid of the error, replace return response.trim(); with return response;. Since you are already processing the JSON response in your activity's onCreate using parseJSON(data), I think that should fix the problem.

Let me know if it does.

Edit

Try removing the ResponseHandler and executing the request without it:

HttpResponse response = httpclient.execute(httppost);
String responseString = EntityUtils.toString(response.getEntity());
int responseCode = response.getStatusLine().getStatusCode();

Also, change the way you instantiated the nameValuePairs. Remove the "2" and just instantiate it like this:

nameValuePairs = new ArrayList<NameValuePair>();

Edit 2

You should always "sanitize" the user's input. In your case, you needed to confirm that the user's input was not empty.

String inputUserString = inputUser.getText().toString().trim();

if (!inputUserString.isEmpty()) {
    // execute the httppost request here

} else {
    // display an error message to the user

}
Sign up to request clarification or add additional context in comments.

16 Comments

Error:(178, 49) error: incompatible types: HttpResponse cannot be converted to String when i try remove the ResponseHandler . i change the way i instantiated the nameValuePairs and still get the same error.
I see where your problem is. response is of type HttpResponse and not String. I have updated my answer.
the error is still the same, i still getting null.. But i found something, if i remove nameValuePairs , i will get the respons, but i need nameValuePairs to parse my inputUser to my php file. Do you have any idea how to solve this?
How did you declare nameValuePairs and inputUser?
This is how i declared it .EditText inputUser; List<NameValuePair> nameValuePairs; inputUser =(EditText) findViewById(R.id.inputId);
|
0

Before you use 'response.trim()', you need to check if your response is referring to null:

final String response = httpclient.execute(httppost,responseHandler);
if (response != null) {
    response = response.trim();
}
return response;

1 Comment

if the response is referring to null, what should i do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.