ANDROID -PHP-JSON not getting data from server -
hi new android json...here m sending id php server getting data database not getting value.plz here when click button go next activity , taking id next activity... main activity...
package com.example.mydata; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; public class mainactivity extends activity implements onclicklistener { button tab1,tab2,tab3,tab4; private static final string tag_bid = "id"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tab1=(button)findviewbyid(r.id.tab1); tab1.setonclicklistener(this); tab2=(button)findviewbyid(r.id.tab2); tab2.setonclicklistener(this); tab3=(button)findviewbyid(r.id.tab3); tab3.setonclicklistener(this); tab4=(button)findviewbyid(r.id.tab4); tab4.setonclicklistener(this); } @override public void onclick(view v) { // todo auto-generated method stub switch (v.getid()){ case r.id.tab1: string id = "1"; intent i=new intent(mainactivity.this,table1activity.class); //startactivity(i); i.putextra(tag_bid, id); // starting new activity , expecting response startactivityforresult(i, 100); break; case r.id.tab2: break; case r.id.tab3: break; case r.id.tab4: break; } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); // if result code 100 if (resultcode == 100) { // if result code 100 received // means user edited/deleted product // reload screen again intent intent = getintent(); finish(); startactivity(intent); } } }
here json file..
package com.example.mydata; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.util.list; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; 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.utils.urlencodedutils; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonexception; import org.json.jsonobject; import android.util.log; 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 == "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; } }
here next activity
public class table1activity extends activity { textview txtname; textview txtprice; textview txtdesc; string id; jsonobject product; //string id; //jsonobject product; //string doc; // progress dialog private progressdialog pdialog; // json parser class jsonparser jsonparser = new jsonparser(); // single product us-book/get_product_details.php private static final string url = "http://app-developments.com/jasmita/address-book/get_product_details.php"; // json node names private static final string tag_success = "success"; private static final string tag_user = "address_book"; private static final string tag_id = "id"; private static final string tag_name = "name"; private static final string tag_email = "address"; // jsonarray user = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_table1); toast.maketext(getapplicationcontext(), "welcome", 3000).show(); //new getproductdetails().execute(); intent = getintent(); // getting product id (pid) intent id = i.getstringextra(tag_id); // getting complete product details in background thread new getproductdetails().execute(); // getting complete product details in background thread // save button click event // delete button click event } /** * background async task complete product details * */ class getproductdetails extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(table1activity.this); pdialog.setmessage("loading product details. please wait..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } /** * getting product details in background thread * */ protected string doinbackground(string... params) { // updating ui background thread runonuithread(new runnable() { public void run() { // check success tag int success; try { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("id", id)); // getting product details making http request // note product details url use request jsonobject json = jsonparser.makehttprequest( url, "get", params); // check log json response log.d("single book details", json.tostring()); // json success tag success = json.getint(tag_success); if (success == 1) { // received product details jsonarray productobj = json .getjsonarray(tag_user); // json array // first product object json array product = productobj.getjsonobject(0); // product pid found // edit text txtname = (textview) findviewbyid(r.id.id1); txtprice = (textview) findviewbyid(r.id.name1); txtdesc = (textview) findviewbyid(r.id.add1); // display product data in edittext txtname.settext(product.getstring(tag_id)); txtprice.settext(product.getstring(tag_name)); txtdesc.settext(product.getstring(tag_email)); }else{ // product pid not found } } catch (jsonexception e) { e.printstacktrace(); } } }); return null; } /* /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog once got details pdialog.dismiss(); } } }
here php code
<?php $response = array(); require_once __dir__ . '/db_connect1.php'; // connecting db $db = new db_connect(); // products products table if (isset($_get["id"])) { $id = $_get['id']; // product products table $result = mysql_query("select * address_book id =$id "); if (!empty($result)) { // check empty result if (mysql_num_rows($result) > 0) { $result = mysql_fetch_array($result); $address_book = array(); $address_book["id"] = $row["id"]; $address_book["name"] = $row["name"]; $address_book["address"] = $row["address"]; // success $response["success"] = 1; // user node $response["address_book"] = array(); array_push($response["address_book"], $address_book); // 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 { // required field missing $response["success"] = 0; $response["message"] = "required field(s) missing"; // echoing json response echo json_encode($response); } ?>