php - My android app does not receive json object -
i have been developing android application retrieve data mysql
database wrote php
script extract data database , encoded results json
, when tested php
script generated clear json
array objects in it. in android app wrote codes grab json
array , extract data failed different trials, started grab errors , discovered content being received application text/html , not application/json should expect. magic comes when check content type of php
script google chrome browser clicking menu>tool>developer tools, shows content type application/json set. so stuck in mate , final year project, don't know out of mess.
here log shows content type being received text/html 03-21 09:29:18.187: d/connection success(791): content type: content-type: text/html
here php script
<?php $connection = connect(); function connect() { $dbhost = 'localhost'; $dbuser = 'user'; $dbpassword = 'password'; $con = mysql_connect($dbhost, $dbuser, $dbpassword); if ($con) { mysql_select_db('vpl') or trigger_error(mysql_error()); } return $con; } $result = mysql_query("select * `news` 1", $connection); $dataarray = array(); if (mysql_num_rows($result) > 0) : while ($row = mysql_fetch_array($result)) : $data['newstitle'] = $row['newstitle']; $data['newscontent'] = $row['newscontent']; array_push($dataarray, $data); endwhile; endif; mysql_close($connection); $json = json_encode($dataarray); header('content-type: application/json'); exit($json); ?>
here android codes
package com.jetas.vpl; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import java.util.list; import org.apache.http.header; import org.apache.http.headerelement; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.client.params.httpclientparams; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair; import org.json.jsonarray; import org.json.jsonobject; import android.annotation.suppresslint; import android.app.activity; import android.os.bundle; import android.os.strictmode; import android.util.log; import android.widget.textview; public class test extends activity { @suppresslint("newapi") @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.news_single_post); strictmode.enabledefaults(); getdata(); } public void getdata() { textview textview = (textview) findviewbyid(r.id.newstitle); // textview textview2 = (textview) findviewbyid(r.id.content); // textview.settext("why stubborn"); inputstream isr = null; string result = ""; string url = "http://10.0.2.2/vpl/getnews.php"; try { defaulthttpclient httpclient = new defaulthttpclient(); httpget httpget = new httpget(url); //httpget.addheader("accept", "application/json"); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); isr = httpentity.getcontent(); // textview2.settext("test passed"); log.d("connection success", "content type: " + httpresponse.getentity().getcontenttype()); } catch (exception e) { log.d("connection failed", "error in http connection" + e.tostring()); textview.settext("connection failed"); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( isr, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); // textview2.settext("test passed"); } isr.close(); result = sb.tostring(); // textview2.settext("test passed"); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); textview.settext("buffer reader problem"); } try { string s = "hey"; jsonarray jarray = new jsonarray(result); log.d("parsing json", jarray.tostring()); (int = 0; < jarray.length(); i++) { jsonobject json = jarray.getjsonobject(1); log.d("json object", json.tostring()); log.d("news title", json.getstring("newstitle")); s = s + json.getstring("newstitle"); textview.settext(s); //string newstitle = json.getstring("newstitle"); //textview.settext(newstitle); s = s + "title: " + json.getstring("newstitle") + "\n" + "content: " + json.getstring("newscontent") + "\n\n"; textview.settext(s); } } catch (exception e) { log.e("log_tag", "error parsing data" + e.tostring()); textview.settext("error in parsing data !!!!!"); } } }
try use asynctask. or run in make network call in separate thread , update ui later using runonuithread() method or use handler.
Comments
Post a Comment