Connection issues with android client and java pc server

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

49 Replies - 6830 Views - Last Post: 26 February 2013 - 01:17 AM

#46 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 131
  • Joined: 10-February 13

Re: Connection issues with android client and java pc server

Posted 24 February 2013 - 02:04 PM

i had used break points, I will use the logcat in the morn..

I'll put it within the onlocationchanged first, if that works then I'll put it within the transmit method.

Thanks again for all your help
Was This Post Helpful? 0
  • +
  • -

#47 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 131
  • Joined: 10-February 13

Re: Connection issues with android client and java pc server

Posted 25 February 2013 - 02:19 AM

Hi,

So this morn I used log.d to debug, the onlocationchanged wasn't getting called. So I edited the code slightly, added some more log.d() to see up to what stage was the code working

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class Child extends Activity implements LocationListener {

	private Socket s;
	private PrintWriter p;
	double latitude;
	double longitude;
	String coordinates;
	Location mLocation;
	LocationManager mLocationManager;

	@Override
	public void onCreate(Bundle savedInstanceState)

	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.child);

		mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		// LocationListener mlocListener = new MyLocationListener();
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);

		String locationprovider = mLocationManager.getBestProvider(criteria,
				true);

		mLocation = mLocationManager.getLastKnownLocation(locationprovider);
		mLocationManager.requestLocationUpdates(locationprovider, 5000,
				(float) 2.0, this);

		latitude = mLocation.getLatitude();
		longitude = mLocation.getLongitude();

		coordinates = ("TESTING" + latitude + longitude);

		Log.d("TESTING", "Test");

		Transmit(coordinates);
	}

	/*
	 * public class MyLocatiWonListener implements LocationListener {
	 * 
	 * public void onLocationchanged(Location loc) { Log.d("TESTING", "Test");
	 * 
	 * latitude = loc.getLatitude(); longitude = loc.getLongitude();
	 * 
	 * coordinates = ("TESTING" + latitude + longitude);
	 * 
	 * Transmit(coordinates); }
	 */

	public void onProviderDisabled(String provider) {
		Toast.makeText(getApplicationContext(), "Gps Disabled",
				Toast.LENGTH_SHORT).show();
	}

	public void onProviderEnabled(String provider) {
		Toast.makeText(getApplicationContext(), "Gps Enabled",
				Toast.LENGTH_SHORT).show();
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

	private void Transmit(final String coordinates) {

		new Thread(new Runnable() {
			public void run() {

				Log.d("TRANSMIT", "CALLED");

				// TODO Auto-generated method stub
				try {
					s = new Socket("192.168.3.100", 4444); // connect to
															// server
					Log.d("CONNECTED", "Connected");

					DataOutputStream _OutPut = new DataOutputStream(
							s.getOutputStream());
					_OutPut.writeBytes(coordinates + "\n");
					_OutPut.flush();
					_OutPut.close();

				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}).start();
	}

	public void onLocationchanged(Location location) {
		// TODO Auto-generated method stub

	}

}



So I just put a Log.d in the onCreate which prints ok, I then wanted to ensure that Transmit() was getting called, so I put a Log.d() in there to ensure it was and that prints, then I needed to check the connection, so I put a Log.d() in after the new socket is created and this is where it stops.

I don't get any printout for this in the logcat. Also on the server side I have the checks

	if (s != null) {					
		System.out.println("Connected");


but nothing is happening there either... :whatsthat:

From reviewing the old code (which is still working) and this, I can't see where exactly it's going wrong, can it have something to do with how it's being called?
Was This Post Helpful? 0
  • +
  • -

#48 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Connection issues with android client and java pc server

Posted 25 February 2013 - 07:50 AM

Okay, good.
So, the onLocationchanged is most likely not getting fired because it requires the phone to have you location.
So if you are indoors, then the GPS work get a lock on you, so it wont get fired.

In the new code, is that a new project?
Have you added the permission to use internet?
Was This Post Helpful? 0
  • +
  • -

#49 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 131
  • Joined: 10-February 13

Re: Connection issues with android client and java pc server

Posted 25 February 2013 - 09:00 AM

Hi,

No it's not a new project, I've just modified the code based on a different tutorial. I have internet permission in the manifest.

As it wasn't going into the onlocationchanged I changed the code, my understanding that with my code now is this line

mLocationManager.requestLocationUpdates(locationprovider, 5000,(float) 2.0, this);


that every five seconds it will check the location rather than have to use the onlocationchanged. This is why I used this and it allowed me to put the mlocation.getLatitude(); into the onCreate()

So I know that transmit gets called but I can't connect to the server... >_</>

I wasn't aware that if I were indoors that the gps wouldn't would work.
Was This Post Helpful? 0
  • +
  • -

#50 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 131
  • Joined: 10-February 13

Re: Connection issues with android client and java pc server

Posted 26 February 2013 - 01:17 AM

EndLessMind it's very strange as the socket code is the same as the working example, so I can't see why it isn't working, would you have any ideas?
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4