How to parse android-php json webservice response -
i new android , studying bit. in hard duty pass json objects between android , php.
i have code send json object send json object server. pretty working well. printing in toast also. can please me how read , show response server application ?
my code looks like
package com.test.webservice; import android.util.log; import android.view.menu; import android.app.activity; import android.os.bundle; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import android.widget.textview; import android.widget.toast; import org.apache.http.header; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.responsehandler; import org.apache.http.client.methods.httppost; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.basicresponsehandler; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicheader; import org.apache.http.params.httpconnectionparams; import org.apache.http.protocol.http; import org.apache.http.util.entityutils; import org.json.jsonobject; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); string path = "http://www.mysite.com/app.php"; httpclient client = new defaulthttpclient(); httpconnectionparams.setconnectiontimeout(client.getparams(), 10000); // timeout // limit httpresponse response; jsonobject json = new jsonobject(); try { httppost post = new httppost(path); json.put("service", "google"); json.put("name", "my name"); log.i("jason object", json.tostring()); post.setheader("json", json.tostring()); stringentity se = new stringentity(json.tostring()); se.setcontentencoding((header) new basicheader(http.content_type, "application/json")); post.setentity(se); response = client.execute(post); /* checking response */ if (response != null) { inputstream in = response.getentity().getcontent(); // // data in // // entity string = convertstreamtostring(in); toast.maketext(getapplicationcontext(), a, toast.length_long).show(); } } catch (exception e) { e.printstacktrace(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } private static string convertstreamtostring(inputstream is) { bufferedreader reader = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); string line = null; try { while ((line = reader.readline()) != null) { sb.append(line + "\n"); } } catch (ioexception e) { e.printstacktrace(); } { try { is.close(); } catch (ioexception e) { e.printstacktrace(); } } return sb.tostring(); } }
suppose if have array called response
on php
side below , assume populating data array in way:
$response = array(); $response["intvalue"] = 1234; $response["stringmessage"] = "data server"; echo json_encode($response);// here echoing json response. in inputstream json data. need extract on there , can display according requirement.
extraction @ android side:
here need convert string(which displaying in toast) json object in below way:
jsonobject json = stringtojsonobj(a); // string response int passedinteger = json.getint("intvalue"); string passedstringvalue = json.getstring("stringmessage"); public jsonobject stringtojsonobj(string result) { jsonobject jobj = null; try { jobj = new jsonobject(result); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } return jobj; }
in way, can individual values(passedinteger, passedstringvalue)
, display on whatever views want. hope helps
Comments
Post a Comment