我正在尝试使用凌空在服务器上发布数据,但是当我点击提交按钮时它只给我Volley.ServerError。但是当我调试我的应用程序时,所有参数都获得了值,而不是它们是错误。
public class ConnectWithSpeaker extends AppCompatActivity implements
View.OnClickListener {
LinearLayout linear_layoutcontainer;
Toolbar toolbar;
String url = Constants.SUBMIT_API;
public static final String KEY_NAME = "name";
public static final String KEY_EMAIL = "email";
public static final String KEY_MOBILE = "mobile";
public static final String KEY_COMPANY = "company";
public static final String KEY_SPEAKERID = "speaker_id";
String s_id;
private EditText u_name;
private EditText u_email;
private EditText u_mobile;
private EditText u_company;
private Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect_with_speaker);
linear_layoutcontainer = (LinearLayout) findViewById(R.id.linear_layoutcontainer);
toolbar = (Toolbar) findViewById(R.id.customtoolbar);
TextView title = (TextView) toolbar.findViewById(R.id.title);
title.setText("Connect with Speakers");
toolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
u_name = (EditText) findViewById(R.id.name);
u_email = (EditText) findViewById(R.id.email);
u_mobile = (EditText) findViewById(R.id.mobile);
u_company = (EditText) findViewById(R.id.compny_name);
submit = (Button) findViewById(R.id.connect);
submit.setOnClickListener(this);
s_id = getIntent().getStringExtra("speakerid");
}
private void submitdetails() {
final String name = u_name.getText().toString().trim(); //trim() remove spaces after&before string
final String email = u_email.getText().toString().trim();
final String mobile = u_mobile.getText().toString().trim();
final String company = u_company.getText().toString().trim();
final String speaker_id = s_id;
Toast.makeText(ConnectWithSpeaker.this, "submit details", Toast.LENGTH_SHORT).show();
CustomJSONObjectRequest request2 = new CustomJSONObjectRequest(Request.Method.POST, url, new
Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println("Response........"+response);
if (response.trim().equals("success")) {
Toast.makeText(getApplicationContext(), "Your request is proceed, we will update you with further updates.",
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(ConnectWithSpeaker.this, volleyError.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_NAME, name);
params.put(KEY_EMAIL, email);
params.put(KEY_MOBILE, mobile);
params.put(KEY_COMPANY, company);
params.put(KEY_SPEAKERID, speaker_id);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request2);
}
@Override
public void onClick(View v) {
if (v == submit) {
if (TextUtils.isEmpty(u_name.getText().toString())) {
u_name.setError("Enter Name");
u_name.requestFocus();
}
if (TextUtils.isEmpty(u_email.getText().toString())) {
u_email.setError("Enter Email");
u_email.requestFocus();
}
if ((TextUtils.isEmpty(u_mobile.getText().toString())) || (u_mobile.length() < 10 || u_mobile.length() > 15)) {
u_mobile.setError("Enter valid Mobile No");
u_mobile.requestFocus();
}
if (TextUtils.isEmpty(u_company.getText().toString())) {
u_company.setError("Enter Company Name");
u_company.requestFocus();
} else {
submitdetails();
}
}
}
这是logcat中的错误。
E/Volley: [1515] BasicNetwork.performRequest: Unexpected response code 400 for http://
答案 0 :(得分:0)
我正在使用它。
public class AEJsonRequest extends JsonObjectRequest {
private static int mStatusCode;
public AEJsonRequest(int method, String url, JSONObject jsonRequest,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener) {
super(method, url, jsonRequest, listener, errorListener);
ServerHandler.bwLog("sending params", jsonRequest.toString());
}
public AEJsonRequest(String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener) {
super(url, jsonRequest, listener, errorListener);
}
public static int getStatusCode() {
return mStatusCode;
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
mStatusCode = volleyError.networkResponse.statusCode;
ServerHandler.bwLog("Error Status code", "" + mStatusCode);
VolleyError error = new VolleyError(new String(volleyError.networkResponse.data));
volleyError = error;
}
return volleyError;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
//params.put("Content-Type", "application/x-www-form-urlencoded");
params.put("Accept", "application/json");
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
}
并将其调用
public void submitDetail(HashMap<String, String> hashMap) throws JSONException {
//param is the JSONobject
final AEJsonRequest jsonRequest = new AEJsonRequest(Request.Method.POST, "url", new JSONObject(hashMap),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// if status is true, it will return the json data to its calling activity
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
MAX_TIMEOUT_MS,
0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(jsonRequest);
}
希望这有助于