android - E/log_tag(1601): Error Parsing Data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray -


everytime when start program error:

e/log_tag(1601): error parsing data org.json.jsonexception: value !doctype of type java.lang.string cannot converted jsonarray

i read many things it, don't know how can resolve problem.. please hep me!!! thanks!

java code:

package com.example.vertretungsplannonnenwerth;  import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader;  import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonarray; import org.json.jsonobject;  import android.support.v7.app.actionbaractivity; import android.support.v4.app.fragment; import android.annotation.suppresslint; import android.os.bundle; import android.os.strictmode; import android.util.log; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.textview;   @suppresslint("newapi") public class mainactivity extends actionbaractivity {  textview resultview;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     strictmode.enabledefaults();     resultview = (textview) findviewbyid(r.id.result);     getdata();     }          public void getdata(){     string result = "";     inputstream = null;     try{         httpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost("link");         httpresponse response = httpclient.execute(httppost);         httpentity entity = response.getentity();         = entity.getcontent();     }  catch(exception e){     log.e("log_tag", "keine verbindung zur internetseite"+e.tostring());     resultview.settext("keine verbindung zur datenbank");     }  try{     bufferedreader reader = new bufferedreader(new inputstreamreader(is, "utf-8"),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", "fehler beim konvertieren" +e.tostring()); } try{     string s =result;     jsonarray jarray = new jsonarray(result);       for(int i=0; i<jarray.length(); i++){         jsonobject json = jarray.getjsonobject(i);         s = s +                 "datum : "+json.getint("datum") + "\n" +                 "stunde : "+json.getint("std")+ "\n" +                 "klasse : "+json.getint("klasse")+ "\n" +                 "fach : "+json.getstring("fach")+ "\n" +                 "lehrer : "+json.getstring("lehrer")+ "\n" +                 "vertretung : "+json.getstring("vertretung")+ "\n" +                 "raum : "+json.getint("raum")+ "\n" +                 "bemerkung : "+json.getstring("bemerkung")+ "\n\n";      }     resultview.settext(s);  }  catch (exception e) {     log.e("log_tag", "fehler beim datenabruf"+e.tostring()); }     }  @override public boolean oncreateoptionsmenu(menu menu) {      // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();     if (id == r.id.action_settings) {         return true;     }     return super.onoptionsitemselected(item); }  /**  * placeholder fragment containing simple view.  */ public static class placeholderfragment extends fragment {      public placeholderfragment() {     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         view rootview = inflater.inflate(r.layout.fragment_main, container,                 false);         return rootview;     } } 

}

php code:

$con = mysql_connect("server", "db name", "password");  if (!$con)      {      die ('keine verbindung zur datenbank.': mysql_error());      } mysql_select_db("db", $con);  $result=mysql_query("select*from tbvertretungsplan");  while ($row = mysql_fetch_assoc($result))      {      $output[]=$row;      }  print (json.encode($output));  mysql_close($con);   ?> 

the json string trying convert starts word !doctype. since string !doctype not formatted json string jsonexception.

the link reading data starts html tag <!doctype html> (example). sanitize data input string , remove html tags , non-json strings make code work. can sanitize either in php code (not sure how code provided), or strip out tags in app.

to remove html tags (and other tags) can use:

string nohtml = html.replaceall("\\<.*?>",""); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -