How can I fetch data from a web server in an android application? -
i want retrieve data web server in android application, , don't know begin. should use web services?
i recommend these tutorials:
connect android php , mysql, json in android , php , mysqli
i used these tutorials , managed trying working without difficulty.
between them describe each step in how attempting @ each stage, android application, database , web server side , has information included can process , use received information
the thing add connect android php , mysql tutorial makes use of mysql_ in php deprecated. better use mysqli why included third link.
the basic outline of want this:
1) in android app make request server php script using class this:
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 { // response http request static inputstream httpresponsestream = null; // json response string create json object static string jsonstring = ""; // method issue http request, parse json result , return json object public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { try { // http client defaulthttpclient httpclient = new defaulthttpclient(); // if required http method post if (method == "post") { // create http post object httppost httppost = new httppost(url); // encode passed parameters http request httppost.setentity(new urlencodedformentity(params)); // execute request , fetch http response httpresponse httpresponse = httpclient.execute(httppost); // extract result response httpentity httpentity = httpresponse.getentity(); // open result input stream parsing httpresponsestream = httpentity.getcontent(); } // else if else if (method == "get") { // format parameters correctly http transmission string paramstring = urlencodedutils.format(params, "utf-8"); // add parameters url in format url += "?" + paramstring; // execute request httpget httpget = new httpget(url); // execute request , fetch http response httpresponse httpresponse = httpclient.execute(httpget); // extract result response httpentity httpentity = httpresponse.getentity(); // open result input stream parsing httpresponsestream = httpentity.getcontent(); } // catch possible exceptions } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { // create buffered reader httpresponcestream bufferedreader httpresponsereader = new bufferedreader( new inputstreamreader(httpresponsestream, "iso-8859-1"), 8); // string hold current line httpresponsereader string line = null; // clear jsonstring jsonstring = ""; // while there still more response read while ((line = httpresponsereader.readline()) != null) { // add line jsonstring jsonstring += (line + "\n"); } // close response stream httpresponsestream.close(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } try { // create jsonobject jsonstring , return return new jsonobject(jsonstring); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); // return null if in error return null; } } }
which handles communication, opens connection , receives json string processes json object.
2) in php server, open mysqli connection sql database, run mysqli->query() , following result:
if (mysqli_num_rows($result) > 0) { // looping through results $response["apps"] = array(); while ($row = mysqli_fetch_array($result)) { $apps = array(); $apps["name"] = $row["name"]; $apps["package"] = $row["package"]; $apps["version"] = $row["version"]; $apps["dateversion"] = $row["dateversion"]; $apps["sdkver"] = $row["sdkver"]; $apps["pathroot"] = $row["pathroot"]; $apps["rootname"] = $row["rootname"]; $apps["apkmd5"] = $row["apkmd5"]; $apps["extraapkmd5"] = $row["extraapkmd5"]; $apps["instructionsmd5"] = $row["instructionsmd5"]; $apps["assetsmd5"] = $row["assetsmd5"]; $apps["root"] = $row["root"]; $apps["current"] = $row["current"]; // push single product final response array array_push($response["apps"], $apps); } // success $response["success"] = 1; // echoing json response echo json_encode($response);
this iterates through database response , encodes json string sent android app can process it.
how create explained in tutorials linked
Comments
Post a Comment