0

In Mainactivity.java, I am taking signal strength from wifi 5 times and appending into a service. I pass the arraylist through Intent. I get the value through Intent in the other file. Now, I want to pass this value to java desktop server. I call asynctask method in onStart in Intent. I do not get any error. But no value is getting displayed.

When I do log cat, the value is getting displayed.

Would you please tell me where I am wrong

Main Activity.Java

public class MainActivity extends Activity {  
                    TextView mTextView;
        private WifiManager wifiManager;
                int count =0; String data ="";
        ArrayList<String> arr = new ArrayList<String>();           
        private String messsage;    
        private static final IntentFilter FILTER = new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mTextView = (TextView) findViewById(R.id.text_id);          
            wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);  
            registerReceiver(scanReceiver, FILTER);    
            wifiManager.startScan();
        }


        @Override
        public void onStart() {
            super.onStart();            
            // Register the scan receiver
            registerReceiver(scanReceiver, FILTER);
        }    
        @Override
        public void onStop() {
            super.onStop();
            // Unregister the receiver
            unregisterReceiver(scanReceiver);
        }    
        public void onClickRefresh(View v) {
            count=0;
            wifiManager.startScan();
                    }                   
        BroadcastReceiver scanReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if(count<5){
                 final StringBuilder sb = new StringBuilder();

                 List<ScanResult> wifiScanList = wifiManager.getScanResults();
                 for (ScanResult result : wifiScanList) {
                        if (result.SSID.equals("Dal")) {
                            sb.append(""+result.level);
                        }                
                    } 
                 mTextView.setText("caled" +count + sb + " length is" + sb.length());
                 arr.add(sb.toString());
                count++;
                 wifiManager.startScan();
                }
                else if (count==5)
                {
                     Intent i= new Intent(context, javaServiceClass.class);
                    i.putExtra("stock_list", arr);
                    context.startService(i);
                }
            }
        };

JavaServiceClass.java

    public class javaServiceClass extends Service {

    public void onStart(Intent intent, int startId) {

                    ArrayList<String> stock_list = (ArrayList<String>) intent.getExtras().get("stock_list");

                    System.out.println(stock_list.get(1) );
                    messsage = stock_list.get(0);
                new Asynctask1().execute(messsage);
}
     public class Asynctask1 extends AsyncTask<String, Void, Void> {
                      private PrintWriter printwriter;      
                  protected Void doInBackground(String... messages) {
                        final String IP_ADDRESS = "134.190.162.165";  
                        final int DEST_PORT = 4444;

                      if(messsage !=null){

                        //Socket client = null;
                        try {
                            Socket client = new Socket(IP_ADDRESS, DEST_PORT); // connect to server

                            printwriter = new PrintWriter(client.getOutputStream(), true);
                            printwriter.write(messsage); // write the message to output stream

                            printwriter.flush();
                            printwriter.close();
                            client.close();
                        } catch (UnknownHostException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
      else
                      { System.out.println(messsage);}
                      return null;
user3345483
  • 19
  • 2
  • 7
  • Do you know for a fact that the Android device and the desktop system are on mutually routable networks (typically meaning the same wifi)? Are you sure there is no firewall on the desktop, and that the server is actually running an listening. Add some logging to your code to indicate (un)successful opening of a connection to the desktop. Also add logging to indicate that the message is actually non-null there (and don't try to System.out.println(message) in the case where it is null) – Chris Stratton Feb 24 '14 at 21:11
  • @ChrisStratton , I have disabled the firewall. If I execute only Asynctask with a texbox and a button, when i click on a button the text from textbox is displayed in javadesktop server. – user3345483 Feb 24 '14 at 21:32
  • Then you need to put in some logging to track down the difference between the working and the non-working cases. – Chris Stratton Feb 24 '14 at 23:00

1 Answers1

0

I think You should return your value to onPostExecute() method of AsyncTask class and there try print your value, some thing like this:

@Override
protected void onPostExecute(String messsage) {      
System.out.println(messsage);
}

just return your message and print It here

Hamid Reza
  • 624
  • 7
  • 23