Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use the following steps to get Value from PHP server</p> <p>from your Activity call</p> <pre><code>JSONObject mJsonObject = new JSONObject(); mJsonObject.put("username", "your username"); mJsonObject.put("password", "Password"); JSONOBJECT json = HttpClient.SendHttpPost("http://Abc.com/login.php",mJsonObject); </code></pre> <p>Log.e("Result JSON from Server", ""+json); Parse the above JSON.</p> <p>HttpClient.java </p> <pre><code>public class HttpClient { private static final String TAG = "HttpClient"; public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) { try { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpPostRequest = new HttpPost(URL); StringEntity se; se = new StringEntity(jsonObjSend.toString()); httpPostRequest.setEntity(se); httpPostRequest.setHeader("Accept", "application/json"); httpPostRequest.setHeader("Content-type", "application/json"); httpPostRequest.setHeader("Accept-Encoding", "gzip"); long t = System.currentTimeMillis(); HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest); Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]"); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); Header contentEncoding = response.getFirstHeader("Content-Encoding"); if (contentEncoding != null &amp;&amp; contentEncoding.getValue().equalsIgnoreCase("gzip")) { instream = new GZIPInputStream(instream); } String resultString= convertStreamToString(instream); instream.close(); resultString = resultString.substring(0,resultString.length()-1); JSONObject jsonObjRecv = new JSONObject(resultString); Log.i(TAG,"&lt;JSONObject&gt;\n"+jsonObjRecv.toString()+"\n&lt;/JSONObject&gt;"); return jsonObjRecv; } } catch (Exception e) { Log.e("Exception", "Exception"); e.printStackTrace(); } return null; } 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(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } } </code></pre> <p>server PHP code login.php</p> <pre><code>&lt;?php //echo "asdsa"; include("config.php"); $json = file_get_contents('php://input'); $obj = json_decode($json); $username = $obj-&gt;{'username'}; $password = $obj-&gt;{'password'}; //$username = 'username'; //$password = '123456'; // echoing JSON response $sql="SELECT id,email,first_name,last_name FROM tbl_users WHERE username='".mysql_escape_string($username)."' and password='".mysql_escape_string($password)."'"; $result=mysql_query($sql); $response = array(); if(mysql_num_rows($result) &gt;0) { $user_rs = mysql_fetch_array($result); $user_id=$user_rs['id']; $response['status'] = 'pass'; $response['userId'] = $user_id; } else { $response['status'] = 'fail'; $response['userId'] = 0; } header('Content-type: application/json'); echo json_encode($response); ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload