java - Converting JSON to String in Android with MySql project -


i'm new on android , i'm trying connect android application local server phpmyadmin. i've seen tutorial interaction android-mysql through php pages (using json), have problem output (how convert obtained json string, example?). make better understand problem, code:

jsonparser.java

public class jsonparser {  static inputstream = null; static jsonobject jobj = null; static string json = "";  // constructor public jsonparser() {  }  // function json url // making http post or method 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;  } } 

this php file "get_product.php" (the db connection works fine):

<?php  $response = array(); require_once __dir__ . '/db_connect.php'; $db = new db_connect();  if (isset($_get["id"])) { $id_work = $_get['id'];  $result = mysql_query("select * products id = $id");  if (!empty($result)) {     if (mysql_num_rows($result) > 0) {          $result = mysql_fetch_array($result);          $product = array();         $product["id"] = $result["id"];         $product["title"] = $result["title"];         $product["description"] = $result["description"];          // success         $response["success"] = 1;          // user node         $response["product"] = array();          array_push($response["product"], $product);          // echoing json response         echo json_encode($response);     } else {         // no product found         $response["success"] = 0;         $response["message"] = "no product found";          // echo no users json         echo json_encode($response);     } } else {     // no product found     $response["success"] = 0;     $response["message"] = "no product found";      // echo no users json     echo json_encode($response); } } else { $response["success"] = 0; $response["message"] = "required field(s) missing";  echo json_encode($response); }  ?> 

in manifest insert internet permissions , now, in activity insert:

private static string url = "http://ip_address/connect/get_product.php"; 

but how can save title , description obtained in string?

thank indeed help!

@sajmon: logcat

05-13 19:53:32.322: e/json parser(10336): error parsing data org.json.jsonexception: value <html><head><title> of type java.lang.string cannot converted jsonobject 05-13 19:53:32.322: d/androidruntime(10336): shutting down vm 05-13 19:53:32.322: w/dalvikvm(10336): threadid=1: thread exiting uncaught exception (group=0x4001d578) 05-13 19:53:32.332: e/androidruntime(10336): fatal exception: main 05-13 19:53:32.332: e/androidruntime(10336): java.lang.runtimeexception: unable start activity componentinfo{package/package.myactivity}: java.lang.nullpointerexception 05-13 19:53:32.332: e/androidruntime(10336):    @ android.app.activitythread.performlaunchactivity(activitythread.java:1659) 05-13 19:53:32.332: e/androidruntime(10336):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:1675) 05-13 19:53:32.332: e/androidruntime(10336):    @ android.app.activitythread.access$1500(activitythread.java:121) 05-13 19:53:32.332: e/androidruntime(10336):    @ android.app.activitythread$h.handlemessage(activitythread.java:943) 05-13 19:53:32.332: e/androidruntime(10336):    @ android.os.handler.dispatchmessage(handler.java:99) 05-13 19:53:32.332: e/androidruntime(10336):    @ android.os.looper.loop(looper.java:138) 05-13 19:53:32.332: e/androidruntime(10336):    @ android.app.activitythread.main(activitythread.java:3701) 05-13 19:53:32.332: e/androidruntime(10336):    @ java.lang.reflect.method.invokenative(native method) 05-13 19:53:32.332: e/androidruntime(10336):    @ java.lang.reflect.method.invoke(method.java:507) 05-13 19:53:32.332: e/androidruntime(10336):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:878) 05-13 19:53:32.332: e/androidruntime(10336):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:636) 05-13 19:53:32.332: e/androidruntime(10336):    @ dalvik.system.nativestart.main(native method) 05-13 19:53:32.332: e/androidruntime(10336): caused by: java.lang.nullpointerexception 05-13 19:53:32.332: e/androidruntime(10336):    @ package.myactivity.oncreate(myactivity.java:158) 05-13 19:53:32.332: e/androidruntime(10336):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 05-13 19:53:32.332: e/androidruntime(10336):    @ android.app.activitythread.performlaunchactivity(activitythread.java:1623) 05-13 19:53:32.332: e/androidruntime(10336):    ... 11 more 

and line 158 is:

string title = obj.getstring("title"); 

so got it. makehttprequest() method returns jsonobject returned php script.

now need parse json , retrieve , save values it.

here solution pseudo-code:

jsonparser parser = new jsonparser(); jsonobject obj = parser.makehttprequest("get", <url>, <params>); // getting data json string title = obj.getstring("title"); string decription = obj.getstring("description"); 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -