0 Replies - 662 Views - Last Post: 06 March 2012 - 11:12 AM

#1 deucalion0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 27-November 10

What is the best way to retrieve data from a database using HTTP Post?

Posted 06 March 2012 - 11:12 AM

I have created a way of entering data into my database table using android and now I am trying to retrieve it and display it onto the screen. Here is the code I have so far for my viewscores class:


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));



	    }
	
     
         }




I did spend the whole day trying to use JSON to output my results to the screen but it was too difficult too manage, I deleted this code but here is what I did have for the loadRemote method:

	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());
        }
 
     
   //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 ct_id;
     String ct_name;
     try{
           jArray = new JSONArray(result);
           JSONObject json_data=null;
           for(int i=0;i<jArray.length();i++){
                  json_data = jArray.getJSONObject(i);
                  ct_id=json_data.getInt("score");
                  ct_name=json_data.getString("username");
              }
           }
           catch(JSONException e1){
         	  Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
           } catch (ParseException e1) {
     			e1.printStackTrace();
     	}
     }


I know my query works, you can see it here

So I want when I press a button that it displays the username and scores onto the screen. I would really appreciate any help with this at all as I am completely stuck. Getting the data into the database was difficult for me but this is proving to be impossible. The last part is just displaying the returned query data.

Many thanks!!!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1