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.
Here is the complete HTTP code:
public void LoadRemote()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//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<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();
}
}
Here is my SQL query:
$sql=mysql_query("select score, username from highscores ");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
You can see this working here
I would appreciate any advice on this issue.
Thanks.
I have started again due to complications, here is the activity class as it is now:
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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
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));
}
}
ok.. here is the entire code …
here is the “lst” class
This one works for sure… try this… 🙂