0

The following code is not receiving the output of the PHP file which is "badminton society" from this piece of code in the PHP file:

print(json_encode($row['Description']));

It would be great if someone can spot the mistake. Both 'result' and 'is' are empty variables:

public class BackgroundAsyncTask extends AsyncTask<Void, String, Void> {

  protected Void doInBackground(Void... params) {
    //http post
    try{
      HttpClient client = new DefaultHttpClient();  
      HttpGet get = new HttpGet("http://ntusocities.host22.com/post.php");
      HttpResponse response = client.execute(get);  
      HttpEntity entity = response.getEntity();
      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 {
      JSONObject userObject = new JSONObject(result);
      info = userObject.getString("Description");
    }
    catch(Exception ex){
    }   
    return null;
  }
}

Thanks a lot!

Trinimon
  • 13,839
  • 9
  • 44
  • 60
Ryaller
  • 77
  • 3
  • 12
  • 1
    What error are you getting exactly? – Johnathan Au Apr 10 '13 at 17:15
  • Check the response given from your PHP script to ensure that data is being returned. Try running your script in your browser to check that data is being passed. – kabuto178 Apr 10 '13 at 17:17
  • @kabuto178 I have ran the PHP in my browser and I get the result, it just doesn't log it in the app – Ryaller Apr 10 '13 at 17:21
  • @JohnathanAu I dont get an actual error, its just the 'is' and 'result' are null – Ryaller Apr 10 '13 at 17:23
  • Download POSTMAN chrome web browser extension. Test your URLS and parameters in there and see what you return. Here, you can test your server app separately. Are you sure you're returning a JSON object from the server and not a string? I just want you to double check your server-side scripts. I usually forget silly things and think it's a problem with the Android app. Post the server script as well. We must see an error there that you might have missed. – Johnathan Au Apr 10 '13 at 17:33
  • https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en – Johnathan Au Apr 10 '13 at 17:33

3 Answers3

1

Your url http://ntusocities.host22.com/post.php returns badminton society string. This is not a valid JSON, so it is not a surprise that JSON parsing fails. You need to fix the server side to return a valid JSON.

What is JSON? -> http://en.wikipedia.org/wiki/JSON

How to check if JSON is valid? -> http://jsonlint.com/

Also, there is a very simple way to get response as string: https://stackoverflow.com/a/4480517/247013

Community
  • 1
  • 1
Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
0

Did u add permission in AndroidManifest.xml

<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

The link you mentioned, giving the data

{"info":[{"SocietyID":"SOC003","Name":"Badminton","Type":"Sport","President":"Me","VicePresident":"You","ContactEmail":"me@gmail.com","Description":"badminton society"}]}<!-- some data-->

Just remove, '[' and ']' and "<!-- some data-->" from your response. I think the "some data part    is comment.

everything else is fine.

then try this code to get the values,

JSONObject userObject;
try {
userObject = new JSONObject(content);
JSONObject info = userObject.getJSONObject("info");
String name = info.getString("Name");
String type = info.getString("Type");
String description = info.getString("Description");
    // like this fetch all value/fields

Log.i("JOSN", name +", "+ type +", "+ description);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

All the best !

surender8388
  • 474
  • 3
  • 12
  • Try this once............ String url = "http://ntusocities.host22.com/post.php"; URLConnection conexion = url.openConnection(); conexion.connect(); InputStream input = new BufferedInputStream(url.openStream()); //read it with BufferedReader BufferedReader br = new BufferedReader(new InputStreamReader(input)); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); input.close(); – surender8388 Apr 10 '13 at 17:49
  • thanks a lot but it is giving me errors on the lines 'URLConnection conexion = url.openConnection();' and 'InputStream input = new BufferedInputStream(url.openStream());' – Ryaller Apr 11 '13 at 06:54
  • The code works fine for me and I am able to fetch the desired data from the link you mentioned. Can you please tell me what error you getting exactly? or just try some hello world app which fetches data from a given http link. So you will come to know that, the problem is with code or with your connection. – surender8388 Apr 11 '13 at 13:55
  • yeah, it seems to now. Just gotta parse it now... but struggling – Ryaller Apr 12 '13 at 12:43
  • If You struggling with JSON, Just let me know.... I am also using Json in my Project, If possible will help you out. – surender8388 Apr 12 '13 at 13:38
  • yeah, I really am. That would be great if you could drop me an email or something. zach.ryall16@gmail.com – Ryaller Apr 14 '13 at 16:10
  • For json, you can go through tutorials, – surender8388 Apr 14 '13 at 16:40
  • i put that in a doinbackground with the HTTP code and logcat is giving me an 'org.json.JSONArray cannot be converted to a JSONObject' error... – Ryaller Apr 14 '13 at 20:08
  • I checked it on my system, Its working fine for me. In your http response some extra comments are there, Please remove that as I mentioned in my answer. It must work. I think you didn't remove '[' and ']' from your http response. – surender8388 Apr 15 '13 at 03:36
  • Just once display your http response text in a textView, you will have the idea that what you need and what you don't need. – surender8388 Apr 15 '13 at 03:38
0

well, the output from the url is:

"badminton society"
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

means it is not JSON, like Arhimend already mentioned. But this should not stop you from getting a valid InputStream. Try this:

protected Void doInBackground(Void... params) {
//http post
try{
  is = new java.net.URL("http://ntusocities.host22.com/post.php").openStream();
} catch(Exception e){
  Log.e("log_tag", "Error in http connection ", e);
}
...

Anyway, the code will fail parsing at

 JSONObject userObject = new JSONObject(result);

because the "result" is not JSON.

comeGetSome
  • 1,913
  • 19
  • 20