0 Replies - 1404 Views - Last Post: 07 September 2012 - 10:04 AM

#1 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

Problem passing result data from AsysncTask

Posted 07 September 2012 - 10:04 AM

I wanted to pass the results of AsysncTask to onPostExecute function to display on the UI, and the data should be coming from a PHP file which will interact with the database then echo results, sadly data variable is keeping null, i am not sure if this is set up properly but this is the code that I have so far:

package com.example.httptest;

public class HTTPActivity extends Activity  {
  
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http);
       
        
      
        
        /*  ConnectivityManager cm = (ConnectivityManager) 
		    getSystemService(Context.CONNECTIVITY_SERVICE);
		    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		    // if no network is available networkInfo will be null
		    // otherwise check if we are connected
		    if (networkInfo != null && networkInfo.isConnected()) {
		    	
		    }*/	
        new Testing().execute();
       
       
    }
    
    
    public class Testing extends AsyncTask<String, Integer, String> {
    	TextView data_details;
    		   
    		 /*CALLS THE CONNECTIONS CLASS
               Intent connections = new Intent(HTTPActivity.this,DisplayActivity.class);
               startActivity(connections);*/

    		    
		@Override
		protected String doInBackground(String... params) {
			
			
			HttpPost httppost;
  	       HttpResponse response;
  	       HttpClient httpclient;
  	       InputStream inputStream;
  	       List<NameValuePair> nameValuePairs;  
  		   BufferedReader in = null;
 		   String data = new String();
		    
			try {
    
   				httpclient = new DefaultHttpClient();
   				httppost = new HttpPost("http://10.0.2.2/PDO/droid.php");
   				
   				
   			    // Add your data
   			    nameValuePairs = new ArrayList<NameValuePair>(2);
   			    nameValuePairs.add(new BasicNameValuePair("UserEmail", "nove"));
   			    nameValuePairs.add(new BasicNameValuePair("Password", "jerome"));
   			    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
   			    
   			    
   			    
   			    // Execute HTTP Post Request
   			    response = httpclient.execute(httppost);
   			    
   			    inputStream = response.getEntity().getContent();	    
   			 
   			    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
   			    StringBuffer sb = new StringBuffer();
   			    String line = "";
   			    String new_line = System.getProperty("line.separator");
   			    
   			    while((line = in.readLine()) != null){
   			    	sb.append(line + new_line);
   			    	
   			    }
   			    in.close();
   			    data = sb.toString();
   			    
   			    
   			    
   				}catch(Exception e){
   									Log.e("LOG THIS", e.toString());
   								}  
			return data;
		}
		
		@Override
		  protected void onPostExecute(String result) {
		   // TODO Auto-generated method stub
		   super.onPostExecute(result);
		   Toast.makeText(HTTPActivity.this, "Done syncing"+result, Toast.LENGTH_LONG).show();
		   TextView data_details = (TextView)findViewById(R.id.http_data_details);
	       data_details.setText("DATA->" + result);
		  }
		
		
       };//END TESTING CLASS	
        
   

    
		
		
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_http, menu);
        return true;
    }
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1