2017-02-17 9 views
0

私はボレーを使用してサーバーにデータを投稿しようとしていますが、SubmitボタンをクリックするとVolley.ServerErrorが表示されます。また、私のアプリケーションをデバッグすると、エラーです。Volleyを使用してデータリクエストを投稿

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

あなたはPOSTMANを使用して、サーバー上の同じ要求を投稿しようとしている助け

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); } 

希望にそれを呼び出しますか?サーバー側からの応答が成功しているかどうかを確認します。私はあなたのコードを通過していないが、少なくともあなたの不具合やサーバーの問題があるかどうかを確認してください。 –

+0

これはサーバ側の問題だと思われます。代わりに一度投稿してください。http://posttestserver.com/ – Nilabja

+0

私は郵便配達員のAPIを打ちました。 { "ステータス":false、 "というメッセージ「:」

名フィールドが必要とされて

\ nは

会社名]フィールドが必要とされて

\ n個

メールフィールドが必要とされる

\ n個

スピーカーidフィールドが必要とされて

\ nは

モバイル番号フィールド。。。。が必要です。

\ n " " と私はすべての値を入れました。しかし、デバッガは私にそれぞれの値を表示するので混乱しています私は私のアプリを実行すると、printresponseをスキップするか、Errorステートメントで直接ジャンプします。 –

答えて

0

私はこれを使用しています。 this'd

関連する問題