android - Error parsing data erorr in org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject -
i have error json
, jsonobject
cannot release jsonarray
,
okay logcat.
error parsing data org.json.jsonexception: value <br of type java.lang.string cannot converted jsonobject 08-18 18:38:17.079 31156-31156/baru.azoel.ayomuncak w/system.err: org.json.jsonexception: no value isi 08-18 18:38:17.082 31156-31156/baru.azoel.ayomuncak w/system.err: @ org.json.jsonobject.get(jsonobject.java:389) 08-18 18:38:17.082 31156-31156/baru.azoel.ayomuncak w/system.err: @ org.json.jsonobject.getstring(jsonobject.java:550) 08-18 18:38:17.082 31156-31156/baru.azoel.ayomuncak w/system.err: @ baru.azoel.ayomuncak.detailberita$ambildetailberita$1.run(detailberita.java:91) 08-18 18:38:17.082 31156-31156/baru.azoel.ayomuncak w/system.err: @ android.os.handler.handlecallback(handler.java:739) 08-18 18:38:17.082 31156-31156/baru.azoel.ayomuncak w/system.err: @ android.os.handler.dispatchmessage(handler.java:95) 08-18 18:38:17.082 31156-31156/baru.azoel.ayomuncak w/system.err: @ android.os.looper.loop(looper.java:148) 08-18 18:38:17.082 31156-31156/baru.azoel.ayomuncak w/system.err: @ android.app.activitythread.main(activitythread.java:5441) 08-18 18:38:17.082 31156-31156/baru.azoel.ayomuncak w/system.err: @ java.lang.reflect.method.invoke(native method) 08-18 18:38:17.082 31156-31156/baru.azoel.ayomuncak w/system.err: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:738) 08-18 18:38:17.082 31156-31156/baru.azoel.ayomuncak w/system.err: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:628)
column "isi"in database use field text not varchar, ok detailberita.java
package baru.azoel.ayomuncak; import android.app.activity; import android.app.progressdialog; import android.content.intent; import android.os.asynctask; import android.os.bundle; import android.widget.imageview; import android.widget.textview; import android.widget.toast; import com.squareup.picasso.picasso; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.util.arraylist; import java.util.list; import baru.azoel.ayomuncak.koneksi.imageloaderberita; import baru.azoel.ayomuncak.koneksi.jsonparserberita; public class detailberita extends activity { public imageloaderberita imageloader; { imageloader = new imageloaderberita(null); } jsonarray string_json = null; string idberita; private progressdialog pdialog; jsonparserberita jsonparser = new jsonparserberita(); public static final string tag_id = "id_berita"; public static final string tag_judul = "judul"; public static final string tag_isi = "isi"; public static final string tag_gambar = "gambar"; private static final string url_detail_berita = "https://marimuncak.000webhostapp.com/detailberita.php?"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.detail_berita); intent = getintent(); idberita = i.getstringextra(tag_id); toast.maketext(getapplicationcontext(), "id_berita = " + idberita, toast.length_short).show(); new ambildetailberita().execute(); } class ambildetailberita extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(detailberita.this); pdialog.setmessage("mohon tunggu ... !"); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } protected string doinbackground(string... params) { try { list<namevaluepair> params1 = new arraylist<namevaluepair>(); params1.add(new basicnamevaluepair("id_berita",idberita)); jsonobject json = jsonparser.makehttprequest( url_detail_berita, "get", params1); string_json = json.getjsonarray("berita"); runonuithread(new runnable() { public void run() { imageview thumb_image = (imageview) findviewbyid(r.id.imageview1); textview judul = (textview) findviewbyid(r.id.judul); //textview detail = (textview) findviewbyid(r.id.detail); textview isi = (textview) findviewbyid(r.id.content); try { // ambil objek member pertama dari json array jsonobject ar = string_json.getjsonobject(0); string judul_d = ar.getstring(tag_judul); string isi_d = ar.getstring(tag_isi); string url_detail_image = ar.getstring(tag_gambar); judul.settext(judul_d); isi.settext(isi_d); // imageloader.displayimage(ar.getstring(tag_gambar),thumb_image); picasso.with(getapplicationcontext()) .load(url_detail_image) .error(r.drawable.error) .into(thumb_image); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (exception e){ e.printstacktrace(); } } }); } catch (jsonexception e) { e.printstacktrace(); } return null; } protected void onpostexecute(string file_url) { pdialog.dismiss(); } } }
and jsonparserberita.java
public class jsonparserberita { static inputstream = null; static jsonobject jobj = null; static string json = ""; public jsonparserberita() { } // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request try { // check request method if (method == "post") { // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } else if (method == "get") { // request method defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } 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(); json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
when test json
in php
work, in java not work, in lenter image description here
what can do? php code
<?php $kd=""; include "kon.php"; $kd = $_get['idberita']; if(isset($_get['id_berita'])){ $kd=$_get['id_berita']; } $query = mysqli_query($con, "select * berita berita id_berita='".$kd."'"); $json = '{"berita": ['; while ($row = mysqli_fetch_array ($query)) { //tanda kutip dua (") tidak diijinkan oleh string json, //maka akan kita replace dengan karakter ` //strip_tag berfungsi untuk menghilangkan tag-tag html pada string $char = '"'; $json .= '{"id_berita":"'.$row['id_berita'].'", "id_pos":"'.$row['id_pos'].'", "judul":"'.str_replace($char,'`',strip_tags($row['judul'])).'", "isi":"'.str_replace($char,'`',strip_tags($row['berita'])).'", "gambar":"https://marimuncak.000webhostapp.com/images/'.$row['gambar_berita'].'"},'; } // buat menghilangkan koma diakhir array $json = substr($json,0,strlen($json)-1); $json .= ']}'; // print json echo $json; ?>
Comments
Post a Comment