我正在创建一个将用户坐标发送到数据库的Android应用。但是当我运行它时我会遇到错误 我得到的错误是:
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);
final String ulat =String.valueOf(mGPS.getLatitude());
final String ulong =String.valueOf(mGPS.getLongitude());
if (mGPS.canGetLocation){
mGPS.getLocation();
/*
Toast.makeText(getApplicationContext(),ulat,Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(),ulong,Toast.LENGTH_LONG).show();
*/
}else {
Toast.makeText(getApplicationContext(),"Turn on your GPS/Location ", Toast.LENGTH_LONG).show();
}
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);
}
});
}
}
答案 0 :(得分:0)
尝试添加:
stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
之前
MySingleton.getInstance(MainActivity.this).addTorequestqueue(stringRequest);