php - Getting data from a server with BasicValuePairName? -


i'm using jsonparser class create json sended server, need use receive information now, don't know how it, i'm noob, sorry. create json next part of code, , following class.

// building parameters             params = new arraylist<namevaluepair>();             params.add(new basicnamevaluepair("userid", thought.getuser()));             params.add(new basicnamevaluepair("timestamp", "" + thought.gettimestamp()));             params.add(new basicnamevaluepair("message", thought.getmessage()));             params.add(new basicnamevaluepair("address", thought.getaddress()));             params.add(new basicnamevaluepair("latitude", "" + thought.getlatitude()));             params.add(new basicnamevaluepair("longitude", "" + thought.getlongitude()));              // getting json object             // note create product url accepts post method             jsonparser jsonparser = new jsonparser();             jsonobject json = jsonparser.makehttprequest(url_create_thought, "post", params); 

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<namevaluepair> params) {          // making http request         try {              // check request method             if(method.equalsignorecase("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.equalsignorecase("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.tostring());         } catch (jsonexception e) {             log.e("json parser", "error parsing data " + e.tostring());         }          // return json string         return jobj;      } } 

the part in server think it's ok.

server values part

<?php  /*  * following code list products  */  // array json response $response = array();  // include db connect class require_once __dir__ . '/db_connect.php';  // connecting db $db = new db_connect();  // products products table $result = mysql_query("select * thoughts") or die(mysql_error());  // check empty result if (mysql_num_rows($result) > 0) {     // looping through results     // products node     $response["thoughts"] = array();      echo '<center><div class="datagrid"><table>';     echo '<thead><tr><th>id</th><th>userid</th><th>timestamp</th><th>message</th><th>address</th><th>latitude</th><th>longitude</th></tr></thead><tbody>';      while ($row = mysql_fetch_array($result)) {         // temp user array         $thought = array();         $thought["id"] = $row["id"];         $thought["userid"] = $row["userid"];         $thought["timestamp"] = $row["timestamp"];         $thought["message"] = $row["message"];         $thought["address"] = $row["address"];         $thought["latitude"] = $row['latitude'];         $thought["longitude"] = $row['longitude'];          echo '<tr><td>'.$row['id'].'</td><td>'.$row['userid'].'</td><td>'.$row['timestamp'].'</td><td>'.$row['message'].'</td><td>'.$row['address'].'</td><td>'.$row['latitude'].'</td><td>'.$row['longitude'].'</td></tr>';          // push single product final response array         array_push($response["thoughts"], $thought);     }     // success     $response["success"] = 1;      // echoing json response     //echo json_encode($response);     echo '</table></center>'; } else {     // no products found     $response["success"] = 0;     $response["message"] = "no events found";      // echo no users json     echo json_encode($response); }  ?> 

what it's correct way data in android?? need put in params?

jsonparser jsonparser = new jsonparser(); jsonobject json = jsonparser.makehttprequest(url_create_thought, **"get"**, params); 

thanks help.

here simple way u want json-return url :

string surl = "http://freegeoip.net/json/"; //just string  // connect url using java's native library url url = new url(surl); httpurlconnection request = (httpurlconnection) url.openconnection(); request.connect();  // convert json object print data jsonparser jp = new jsonparser(); //from gson jsonelement root = jp.parse(new inputstreamreader((inputstream) request.getcontent())); //convert input stream json element jsonobject rootobj = root.getasjsonobject(); //may array, may object.  zipcode=rootobj.get("zipcode").getasstring();//just grab zipcode 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -