Note that there are some explanatory texts on larger screens.

plurals
  1. POSend POST to a web service and verify some data and send back as JSON
    primarykey
    data
    text
    <p>!<a href="https://i.stack.imgur.com/rJITu.png" rel="nofollow noreferrer"> </a>I am working on a quiz-app that uses a PHP web service to author/host/maintain data for the Android quizes.</p> <p>Heres is the PHP function in question.</p> <p>I am looking for a post in my PHP code.</p> <pre><code>if (isset($_POST['verifyCourse'])){ verifyCourse($_POST['courseCode']);} </code></pre> <p>Any then this points to the function...</p> <pre><code>function verifyCourse($courseCode){ $result = mysql_query("SELECT * FROM Course WHERE CourseCode = \"$courseCode\";"); $rows = array(); while ($r = mysql_fetch_assoc($result)) { $rows = $r; } if($rows == null){ return 0; } else { return json_encode(array('Course' =&gt; $rows)); } } </code></pre> <p>And then on my Android code I am doing this to send a POST to the server called "verifyCourse" but I am getting nothing in return.</p> <p>Android: Function to send HTTP POSTS</p> <pre><code> public ArrayList&lt;HashMap&lt;String, String&gt;&gt; httpPost(List&lt;NameValuePair&gt; valuepair, String code) { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://mydomain.edu/quiz-app/webservice.php"); StringBuilder builder = new StringBuilder(); try { httppost.setEntity(new UrlEncodedFormEntity(valuepair)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); /* Checking response */ if (statusCode == 200) { HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); Log.d("myapp", "response " + response.getEntity()); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { builder.append(line); } }else{ Log.e("POST-return", "Failed to download file"); } } catch (Exception e) { } ArrayList&lt;HashMap&lt;String, String&gt;&gt; results = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); HashMap&lt;String, String&gt; storage = new HashMap&lt;String, String&gt;(); String value; try{ JSONArray jArray = new JSONArray(builder.toString()); for(int i=0; i&lt;jArray.length(); i++){ JSONObject jsonObject = jArray.getJSONObject(i); value=jsonObject.getString(code); storage = new HashMap&lt;String, String&gt;(); storage.put(code, value); results.add(storage); } } catch (Exception e) { } return results; } </code></pre> <p>I then am using it like this to execute the functionality. /// pass a code from other section of app public void getCourseCodesandVerify(String code) {</p> <pre><code> List&lt;NameValuePair&gt; course_info = new ArrayList&lt;NameValuePair&gt;(2); course_info.add(new BasicNameValuePair("verifyCourse",null)); course_info.add(new BasicNameValuePair("courseCode",code)); httpPost(course_info,null); </code></pre> <p>}</p> <p>Any idea why my code just returns nothing...?</p> <p>Heres what I get back for JSON, how do I process this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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