Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to output to a view using JSON in android when retrieving SQL query
    primarykey
    data
    text
    <p>I am trying to use JSON and HTTP client to retrieve data from a table in my database. The code I have seems to be working but it is not outputting anything, but if I change something on purpose in the query the error toast outputs, so I know the code is right as no error outputs. The problem is figuring out how to get the JSON output onto my view screen.</p> <p>Here is the complete HTTP code:</p> <pre><code>public void LoadRemote() { ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://deucalion0.co.uk/getscores.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection"+e.toString()); } //convert response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); sb = new StringBuilder(); sb.append(reader.readLine() + "\n"); String line="0"; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } //paring data int score; String username; try{ jArray = new JSONArray(result); JSONObject json_data=null; for(int i=0;i&lt;jArray.length();i++){ json_data = jArray.getJSONObject(i); score=json_data.getInt("score"); username=json_data.getString("username"); } } catch(JSONException e1){ Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show(); } catch (ParseException e1) { e1.printStackTrace(); } } </code></pre> <p>Here is my SQL query:</p> <pre><code>$sql=mysql_query("select score, username from highscores "); while($row=mysql_fetch_assoc($sql)) $output[]=$row; print(json_encode($output)); </code></pre> <p>You can see this working <a href="http://deucalion0.co.uk/getscores.php" rel="nofollow">here</a></p> <p>I would appreciate any advice on this issue.</p> <p>Thanks.</p> <p>I have started again due to complications, here is the activity class as it is now:</p> <pre><code>public class Viewscores extends Activity{ Button scores; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.viewscores); scores = (Button) findViewById(R.id.bViewscores); scores.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { LoadRemote(); } }); } public void LoadRemote() { String result = ""; ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); InputStream is = null; try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://deucalion0.co.uk/getscores.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection"+e.toString()); } BufferedReader myReader = new BufferedReader(new InputStreamReader(is)); } } </code></pre>
    singulars
    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.
    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