3

I was reading a tutorial about how to connect android to mysql database over the internet using a php interface. I made everything as it said to.

the php code is this:

  $q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
  while($e=mysql_fetch_assoc($q))
  $output[]=$e;

  print(json_encode($output));

  mysql_close();
?>

and the java code is this:

String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));

//http post
try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://example.com/getAllPeopleBornAfter.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
}catch(Exception e){
    Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
    }
    is.close();

    result=sb.toString();
}catch(Exception e){
    Log.e("log_tag", "Error converting result "+e.toString());
}

//parse json data
try{
    JSONArray jArray = new JSONArray(result);
    for(int i=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            Log.i("log_tag","id: "+json_data.getInt("id")+
                    ", name: "+json_data.getString("name")+
                    ", sex: "+json_data.getInt("sex")+
                    ", birthyear: "+json_data.getInt("birthyear")
            );
    }
}
}catch(JSONException e){
    Log.e("log_tag", "Error parsing data "+e.toString());
}

Now the problem is it gives me an error that the variable 'is' doesn't exist! what should I do?

The tutorial is from website helloandroid.com : http://www.helloandroid.com/tutorials/connecting-mysql-database

1

1 Answer 1

2
try{
    InputStream is = entity.getContent();
    ...
    ...
} //'is' goes out of scope here! 

The scope is limited to the first try-catch block, and hence, is is unavailable in the second.

Do:

InputStream is = entity.getContent(); // 'is' now accessible in both try-catch
HttpEntity entity = response.getEntity(); // 'entity' is now accessible in both try-catch
try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://example.com/getAllPeopleBornAfter.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    ...
    ...
}
Sign up to request clarification or add additional context in comments.

6 Comments

is is accessible now but entity is now the problem. entity cannot be resolved is the problem.
@user1290467 Move entity statement out of the try-catch block as well. Do that for everything you need to share between the 2 try-catch.
it is fixed, I used your way and in the end eclipse told me to use InputStream is = null; below class definition. I used that and there is no error.
There is a new problem by the way, after running program in the log: Error converting result java.lang.NullPointerException Error parsing data org.json.JSONException: End of input at character 0 of
That is a different problem altogether. You did not receive any JSON in response.
|

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.