我正在创建一个将用户坐标发送到数据库的Android应用程序。我的代码中没有错误,但是当我运行它时会出现错误。有一个GPSTracker
类来获取纬度和经度。(没有错误)。我得到的错误是:
04-08 13:40:27.673 3036-3036/com.example.benoit.test
W/System.err: com.android.volley.
TimeoutError 04-08 13:40:27.674 3036- 3036/com.example.benoit.test
W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:122) 04-08 13:40:27.674 3036-3036/com.example.benoit.test
W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
这是我的代码:
public class MainActivity extends AppCompatActivity {
private Button emgbutton;
String url = "http://10.0.2.2/work/wfile/test.php";
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emgbutton = (Button) findViewById(R.id.emergency);
builder = new AlertDialog.Builder(MainActivity.this);
emgbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Animation myAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.bounce);
// Use bounce interpolator with amplitude 0.2 and frequency 20
MyBounceInterpolator interpolator = new MyBounceInterpolator(0.2, 20);
myAnim.setInterpolator(interpolator);
emgbutton.startAnimation(myAnim);
GPSTracker mGPS = new GPSTracker(MainActivity.this);
if (mGPS.canGetLocation) {
mGPS.getLocation();
final String ulat =String.valueOf(mGPS.getLatitude());
final String ulong =String.valueOf(mGPS.getLongitude());
StringRequest stringRequest= new StringRequest(
Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
builder.setMessage(response);
AlertDialog alertDialog= builder.create();
alertDialog.show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Could not send ", Toast.LENGTH_LONG).show();
error.printStackTrace();
}
}
){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params= new HashMap<String, String>();
params.put("ulat",ulat);
params.put("ulong",ulong);
return params;
}
};
MySingleton.getInstance(MainActivity.this).addTorequestqueue(stringRequest);
} else {
Toast.makeText(getApplicationContext(),"Turn on your GPS/Location ", Toast.LENGTH_LONG).show();
}
}
});
}
}
答案 0 :(得分:0)
检查您是否写了正确的参数。
params.put( “乌拉特”,乌拉特); //当“ulat”和“ulong”不匹配时会发生这种情况 params.put( “ULONG”,ULONG); //使用表属性
连接错误。如果您使用的是localhost,请检查防火墙是否已关闭。另一个,您的互联网连接无法正常工作。