我正在使用android volley从我的远程mysql数据库访问我的json数据。大多数时候它没有得到任何数据,它总是进入response.errorlistener部分,但有时它工作正常。请告诉我访问Web服务的最有效方法是什么。
05-06 13:18:49.055 18409-18409 / com.goworks.readyworks D / CubicBezierInterpolator:CubicBezierInterpolator mControlPoint1x = 0.2,mControlPoint1y = 0.65,mControlPoint2x = 0.28,mControlPoint2y = 0.97 05-06 13:18:49.055 18409-18409 / com.goworks.readyworks D / CubicBezierInterpolator:CubicBezierInterpolator mControlPoint1x = 0.2,mControlPoint1y = 0.65,mControlPoint2x = 0.28,mControlPoint2y = 0.97 05-06 13:18:49.055 18409-18409 / com.goworks.readyworks D / CubicBezierInterpolator:CubicBezierInterpolator mControlPoint1x = 0.2,mControlPoint1y = 0.65,mControlPoint2x = 0.28,mControlPoint2y = 0.97 05-06 13:18:49.055 18409-18409 / com.goworks.readyworks D / CubicBezierInterpolator:CubicBezierInterpolator mControlPoint1x = 0.2,mControlPoint1y = 0.65,mControlPoint2x = 0.28,mControlPoint2y = 0.97 05-06 13:18:49.085 18409-18805 / com.goworks.readyworks E / Volley:[569] NetworkDispatcher.run:未处理的异常 java.lang.RuntimeException:无法在线程内创建处理程序 没有调用Looper.prepare() java.lang.RuntimeException:无法在线程内创建处理程序 没有调用Looper.prepare() 在android.os.Handler。(Handler.java:200) 在android.os.Handler。(Handler.java:114) 在android.widget.Toast $ TN。(Toast.java:342) 在android.widget.Toast。(Toast.java:99) 在android.widget.Toast.makeText(Toast.java:248) 在com.goworks.readyworks.RegisterActivity $ 5.getParams(RegisterActivity.java:314) 在com.android.volley.Request.getBody(Request.java:468) 在com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:253) 在com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:227) 在com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107) 在com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97) 在com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114) 05-06 13:18:49.235 18409-18409 / com.goworks.readyworks E /注册错误:: com.android.volley.VolleyError: java.lang.RuntimeException:无法在线程内创建处理程序 没有调用Looper.prepare()
if (isValidated()) {
pDialog = new ProgressDialog(RegisterActivity.this);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setTitle("Making a member");
pDialog.setMessage("Please Wait...");
pDialog.setIndeterminate(true);
pDialog.setCancelable(true);
pDialog.show();
userUsername = uname.getText().toString();
userPassword = pass.getText().toString();
emailAddress = email.getText().toString();
userAddress = address.getText().toString();
userContact = contact.getText().toString();
StringRequest stringrequest = new StringRequest(Request.Method.POST, url + "registration.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String code = jsonObject.getString("code");
String message = jsonObject.getString("message");
if (code.equals("reg_success")) {
Toast.makeText(RegisterActivity.this, message, Toast.LENGTH_SHORT).show();
pDialog.cancel();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
} else if (code.equals("reg_failed")) {
// Snackbar.make(v, message, Snackbar.LENGTH_LONG).setAction("Action", null).show();
pDialog.cancel();
Toast.makeText(getApplicationContext(),"Not Registered",Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Something went wrong, Please try again", Toast.LENGTH_SHORT).show();
// Snackbar.make(v, "Something went wrong, Please try again", Snackbar.LENGTH_LONG).setAction("Action", null).show();
pDialog.cancel();
Log.e("Registration Error:", String.valueOf(error));
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("username", userUsername);
params.put("password", userPassword);
params.put("email", emailAddress);
params.put("contact", userContact);
params.put("address", userAddress);
params.put("location", finalCoords);
Toast.makeText(getApplicationContext(), "Data Delievered", Toast.LENGTH_SHORT).show();
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(RegisterActivity.this);
requestQueue.add(stringrequest);
}
答案 0 :(得分:0)
首先通过在onErrorResponse(..){
if (error instanceof TimeoutError) {
Toast.makeText(context,"TimeoutError",Toast.LENGTH_SHORT).show();
} else if (error instanceof NoConnectionError) {
Toast.makeText(context,"NoConnectionError",Toast.LENGTH_SHORT).show();
} else if (error instanceof AuthFailureError) {
Toast.makeText(context,"AuthFailureError",Toast.LENGTH_SHORT).show();
} else if (error instanceof ServerError) {
Toast.makeText(context,"ServerError",Toast.LENGTH_SHORT).show();
} else if (error instanceof NetworkError) {
Toast.makeText(context,"NetworkError",Toast.LENGTH_SHORT).show();
} else if (error instanceof ParseError) {
Toast.makeText(context,"ParseError",Toast.LENGTH_SHORT).show();
}