刚接触编程不久,所以问的问题菜鸟点请不要介意~
这是我将网上找来的源码东拼西凑的,调用GPS获得位置信息,将经纬度通过SOCKET发送到服务端,但是不知道怎么样把得到的数据赋给待发送的字符串,直接用String sendMsg = Double.toString(location.getLatitude())报错了。设置updateStat()的返回值后面也不会调用,求解决办法import java.io.*;
import java.net.*;import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Xml.Encoding;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class TestClient extends Activity implements LocationListener
{
private LocationManager mgr;
private String best;
private TextView view_location;
private Button button_send;
private Button button_connect;
private TextView view_suggest;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view_location= (TextView) findViewById(R.id.location);
updateStat();
findViews();
setListeners();
}
public void updateStat()
{
//取得系统提供的定位服务
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
//额外判断条件类
Criteria criteria = new Criteria();
//取得最佳的定位方式
//GPS打开优先使用GPS,GPS关闭,则使用网络基站定位
best = mgr.getBestProvider(criteria, true);
Location location = mgr.getLastKnownLocation(best); //取得位置。
if (location != null)
{
//打印经纬度。
StringBuffer msg = new StringBuffer();
msg.append("Latitude:");
msg.append(Double.toString(location.getLatitude()));
msg.append(",Longitude:");
msg.append(Double.toString(location.getLongitude()));
Toast.makeText(TestClient.this, msg, Toast.LENGTH_LONG).show();
view_location.setText(msg);
} else
{
Toast.makeText(TestClient.this, "no lcoation found", Toast.LENGTH_LONG).show();
}
} public void onLocationChanged(Location location)
{
Toast.makeText(TestClient.this, location.toString(), Toast.LENGTH_LONG).show();
} public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub } public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub } public void onProviderDisabled(String provider)
{
} @Override
protected void onResume()
{
// resume状态时,更新经纬度
//更新经纬度
super.onResume();
mgr.requestLocationUpdates(best, 60000, 1, this);
} @Override
protected void onPause()
{
super.onPause();
//关闭时,停止更新
mgr.removeUpdates(this);
}
private void findViews() {
button_send = (Button) findViewById(R.id.send);
button_connect = (Button) findViewById(R.id.connect);
view_suggest = (TextView) findViewById(R.id.message);
}
private void setListeners() {
button_connect.setOnClickListener(connect);
button_send.setOnClickListener(send);
}
private Socket sc;
OutputStreamWriter out=null;
private Button.OnClickListener connect = new Button.OnClickListener() {
//按下Connect后的动作
public void onClick(View v) {
view_suggest.setText("已连接至服务器...");
int port=8500;
try{
sc = new Socket("10.0.2.2",port);
}
catch(UnknownHostException e){view_suggest.setText("error");}
catch(IOException e){view_suggest.setText("error");}
}
};
private Button.OnClickListener send = new Button.OnClickListener() {
///按下send后的动作
public void onClick(View v) {
view_suggest.setText("发送数据...");
try{
out = new OutputStreamWriter(sc.getOutputStream());
String sendMsg = Double.toString(location.getLatitude());
out.write(sendMsg);
out.flush();
}catch(IOException e)
{
System.out.println(e.toString());
}
}
};
这是我将网上找来的源码东拼西凑的,调用GPS获得位置信息,将经纬度通过SOCKET发送到服务端,但是不知道怎么样把得到的数据赋给待发送的字符串,直接用String sendMsg = Double.toString(location.getLatitude())报错了。设置updateStat()的返回值后面也不会调用,求解决办法import java.io.*;
import java.net.*;import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Xml.Encoding;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class TestClient extends Activity implements LocationListener
{
private LocationManager mgr;
private String best;
private TextView view_location;
private Button button_send;
private Button button_connect;
private TextView view_suggest;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view_location= (TextView) findViewById(R.id.location);
updateStat();
findViews();
setListeners();
}
public void updateStat()
{
//取得系统提供的定位服务
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
//额外判断条件类
Criteria criteria = new Criteria();
//取得最佳的定位方式
//GPS打开优先使用GPS,GPS关闭,则使用网络基站定位
best = mgr.getBestProvider(criteria, true);
Location location = mgr.getLastKnownLocation(best); //取得位置。
if (location != null)
{
//打印经纬度。
StringBuffer msg = new StringBuffer();
msg.append("Latitude:");
msg.append(Double.toString(location.getLatitude()));
msg.append(",Longitude:");
msg.append(Double.toString(location.getLongitude()));
Toast.makeText(TestClient.this, msg, Toast.LENGTH_LONG).show();
view_location.setText(msg);
} else
{
Toast.makeText(TestClient.this, "no lcoation found", Toast.LENGTH_LONG).show();
}
} public void onLocationChanged(Location location)
{
Toast.makeText(TestClient.this, location.toString(), Toast.LENGTH_LONG).show();
} public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub } public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub } public void onProviderDisabled(String provider)
{
} @Override
protected void onResume()
{
// resume状态时,更新经纬度
//更新经纬度
super.onResume();
mgr.requestLocationUpdates(best, 60000, 1, this);
} @Override
protected void onPause()
{
super.onPause();
//关闭时,停止更新
mgr.removeUpdates(this);
}
private void findViews() {
button_send = (Button) findViewById(R.id.send);
button_connect = (Button) findViewById(R.id.connect);
view_suggest = (TextView) findViewById(R.id.message);
}
private void setListeners() {
button_connect.setOnClickListener(connect);
button_send.setOnClickListener(send);
}
private Socket sc;
OutputStreamWriter out=null;
private Button.OnClickListener connect = new Button.OnClickListener() {
//按下Connect后的动作
public void onClick(View v) {
view_suggest.setText("已连接至服务器...");
int port=8500;
try{
sc = new Socket("10.0.2.2",port);
}
catch(UnknownHostException e){view_suggest.setText("error");}
catch(IOException e){view_suggest.setText("error");}
}
};
private Button.OnClickListener send = new Button.OnClickListener() {
///按下send后的动作
public void onClick(View v) {
view_suggest.setText("发送数据...");
try{
out = new OutputStreamWriter(sc.getOutputStream());
String sendMsg = Double.toString(location.getLatitude());
out.write(sendMsg);
out.flush();
}catch(IOException e)
{
System.out.println(e.toString());
}
}
};
解决方案 »
- org.apache.axis.ConfigurationException: No service named XX is available
- ajax dwr如何存储request对象
- struts1 bean 的简单显示 问题
- jsp for循环使用> 的问题
- 插入大量数据其中一条记录出现异常导致其它语句不执行
- struts如何在新IE窗口打开连接。。
- 急::请问以下程序哪有问题? thanks!!!!!!!!!
- Tomcat连接池首次配置问题
- 用jbuilder做的jsp,如何讲网址中的端口号8080去掉,高分求助,在线等,多谢!
- J2EE 还是 .NET? 给我一个答案!?
- 现在oracle developer2000过时了嘛?
- 新手请教java web一个帐号只能一个用户使用的问题
定义