im trying develope android app connect external server data. have read data .php file can't use in app because throws error.
<?php /** * class file connect database */ // connect database try { $pdo = new pdo('mysql:host=localhost;dbname=mydbname',' user', 'pass'); $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception); $pdo->setattribute(pdo::attr_emulate_prepares, false); } catch(pdoexception $err) { die($err->getmessage()); } $stmt = $pdo->prepare("select * test"); $result = $stmt->execute(); print_r($stmt->fetchall(pdo::fetch_assoc)); ?>
that's php code use read data. , code use in android studio
protected string doinbackground(string... args) { // building parameters list params = new arraylist(); // getting json string url jsonobject json = jparser.makehttprequest(server_php_url, "get", params); // check log cat json reponse log.d("all products: ", json.tostring()); try { // checking success tag int success = json.getint(tag_success); if (success == 1) { // products found // getting array of products products = json.getjsonarray(tag_products); // looping through products //log.i("ramiro", "produtos.length" + products.length()); (int = 0; < products.length(); i++) { jsonobject c = products.getjsonobject(i); // storing each json item in variable string name = c.getstring(tag_name); // creating new hashmap hashmap map = new hashmap(); // adding each child node hashmap key => value map.put(tag_name, name); empresalist.add(map); } } } catch (jsonexception e) { e.printstacktrace(); } return null; }
this jsonparser class:
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method, list 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; } }
this result of json: array ( [0] => array ( [test] => name1 ) [1] => array ( [name] => name2 ) )
the error : e/json parser﹕ error parsing data org.json.jsonexception: value array of type java.lang.string cannot converted jsonobject
i need help. thank in advance.
retrun json object in in background method
protected string doinbackground(string... args) { // building parameters list params = new arraylist(); // getting json string url jsonobject json = jparser.makehttprequest(server_php_url, "get", params); // check log cat json reponse log.d("all products: ", json.tostring()); try { // checking success tag int success = json.getint(tag_success); if (success == 1) { // products found // getting array of products products = json.getjsonarray(tag_products); // looping through products //log.i("ramiro", "produtos.length" + products.length()); (int = 0; < products.length(); i++) { jsonobject c = products.getjsonobject(i); // storing each json item in variable string name = c.getstring(tag_name); // creating new hashmap hashmap map = new hashmap(); // adding each child node hashmap key => value map.put(tag_name, name); empresalist.add(map); } } } catch (jsonexception e) { e.printstacktrace(); } return json;
Comments
Post a Comment