Ryaller
Ryaller

Reputation: 77

ASyncTask HTTP error

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!

Upvotes: 0

Views: 156

Answers (3)

surender8388
surender8388

Reputation: 474

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":"[email protected]","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 !

Upvotes: 0

comeGetSome
comeGetSome

Reputation: 1963

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.

Upvotes: 0

Vit Khudenko
Vit Khudenko

Reputation: 28418

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

Upvotes: 1

Related Questions