2017-05-25 2 views
0
/* I have to send json object on Sever for which I have implemented following things but I am getting volley 400 error. */ 


package com.example.administrator.crmtestapp; 

import android.app.Activity; 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.SearchView; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

import com.android.volley.AuthFailureError; 
import com.android.volley.NetworkResponse; 
import com.android.volley.Request; 
import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.ServerError; 
import com.android.volley.VolleyError; 
import com.android.volley.VolleyLog; 
import com.android.volley.toolbox.HttpHeaderParser; 
import com.android.volley.toolbox.StringRequest; 
import com.android.volley.toolbox.Volley; 

import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 

public class BP_Details extends AppCompatActivity implements View.OnClickListener { 
    SearchView searchView; 
    EditText bpCode, bpName, bpAddress1, bpAddress2, bpCity, bpState, bpCountry, payTerm, 
       zipNum, etarea, emailId, tinNum, cstNum, panNum, mobileNum, customerLocation, salesPersonNum; 
    String code,name,address1,address2,city,state,country,zip,area,email,tin,cst,pan,mobile,cLocation,salesPname,pTerm; 
    Toolbar toolbar; 
    String s="CBS"; 
    String s1="NOIDA"; 
    int currency=1; 
    Button btn; 
    int finalCurrency; 
    String curency; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.bp__details); 
     toolbar= (Toolbar) findViewById(R.id.my_toolbar); 

     setSupportActionBar(toolbar); 
     bpCode = (EditText) findViewById(R.id.bp_code); 
     bpName = (EditText) findViewById(R.id.bp_name); 
     bpAddress1 = (EditText) findViewById(R.id.address1); 
     bpAddress2 = (EditText) findViewById(R.id.address2); 
     bpCity = (EditText) findViewById(R.id.bp_city); 
     bpState = (EditText) findViewById(R.id.bp_state); 
     bpCountry = (EditText) findViewById(R.id.bp_country); 
     payTerm = (EditText) findViewById(R.id.payterm); 
     zipNum = (EditText) findViewById(R.id.zip); 
     etarea = (EditText) findViewById(R.id.area); 
     emailId = (EditText) findViewById(R.id.email_id); 
     /*currency = (EditText) findViewById(R.id.currency);*/ 
     tinNum = (EditText) findViewById(R.id.tin_no); 
     cstNum = (EditText) findViewById(R.id.cst_no); 
     panNum = (EditText) findViewById(R.id.pan_no); 
     mobileNum = (EditText) findViewById(R.id.mobile_num); 
     customerLocation = (EditText) findViewById(R.id.customer_location); 
     salesPersonNum = (EditText) findViewById(R.id.salesperson_no); 
     searchView=(SearchView) findViewById(R.id.sear); 
     btn=(Button)findViewById(R.id.btnAdd); 
     btn.setOnClickListener(this); 
     searchView.requestFocus(); 

     code = bpCode.getText().toString().trim(); 
     name=bpName.getText().toString().trim(); 
     address1=bpAddress1.getText().toString().trim(); 
     address2=bpAddress2.getText().toString().trim(); 
     city=bpCity.getText().toString().trim(); 
     state=bpState.getText().toString().trim(); 
     country=bpCountry.getText().toString().trim(); 

     // pTerm=payTerm.getText().toString(); 

     zip=zipNum.getText().toString().trim(); 
     area=etarea.getText().toString().trim(); 
     email=emailId.getText().toString().trim(); 
     /*curency=currency.getText().toString().trim(); 
     finalCurrency=Integer.parseInt(curency.trim());*/ 
     tin=tinNum.getText().toString().trim(); 
     cst=cstNum.getText().toString().trim(); 
     pan=panNum.getText().toString().trim(); 
     mobile=mobileNum.getText().toString().trim(); 
     cLocation=customerLocation.getText().toString().trim(); 
     salesPersonNum.getText().toString().trim();     
    } 

    @Override 
    public void onClick(View v) { 
     if (v.getId()==R.id.btnAdd){ 
      RequestQueue requestQueue = Volley.newRequestQueue(BP_Details.this); 

      String URL="http://103.75.33.98/BPService/GetAllBPService.svc/SetAllBP"; 

      JSONObject jsonBody = new JSONObject(); 

      try { 

       jsonBody.put("COMPANY_NO",s); 
       jsonBody.put("LOCATION_NO",s1); 
       jsonBody.put("BP_CODE",code); 
       jsonBody.put("NAME",name); 
       jsonBody.put("ADDRESS1",address1); 
       jsonBody.put("ADDRESS2",address2); 
       jsonBody.put("CITY",city); 
       jsonBody.put("STATE",state); 
       jsonBody.put("COUNTRY",country); 
       jsonBody.put("ZIP",zip); 
       jsonBody.put("AREA_CODE",area); 
       jsonBody.put("EMAIL",email); 
       jsonBody.put("CURRENCY",currency); 
       jsonBody.put("TIN_GRN",tin); 
       jsonBody.put("CST_NO",cst); 
       jsonBody.put("PAN_NO",pan); 
       jsonBody.put("SALES_PERSON_NO",salesPname); 
       jsonBody.put("PHONE_NO",mobile); 
       jsonBody.put("LOCATION",cLocation); 
      }catch (JSONException e){ 
       e.printStackTrace(); 
      } 
      final String mRequestBody = jsonBody.toString(); 
      StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        Log.i("response", response); 
       } 

      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e("LOG_VOLLEY", error.toString()); 
        // As of f605da3 the following should work 
        NetworkResponse response = error.networkResponse; 
        if (error instanceof ServerError && response!=null){ 
         try { 
          String res=new String(response.data,HttpHeaderParser.parseCharset(response.headers));    

         }catch (Exception e){ 
          e.printStackTrace(); 
         } 
        } 
       } 
      }){ 
       @Override 
       public String getBodyContentType() { 
        return "application/json;/*charset=utf-8*/"; 
       } 

       @Override 
       public byte[] getBody() throws AuthFailureError { 
        try { 
         return mRequestBody == null ? null : mRequestBody.getBytes("utf-8"); 
        }catch (UnsupportedEncodingException uee) { 
         VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8"); 
         return null; 
        } 
       } 
       @Override 
       protected Response<String> parseNetworkResponse(NetworkResponse response) { 
        String responseString = ""; 
        if (response != null) { 
         responseString = String.valueOf(response.statusCode); 
        } 
        return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response)); 
       } 
      }; 
      requestQueue.add(stringRequest); 
      } 

    } 
} 

Ich verfolgt logcat Info bekommen: Oktober 05-25: 23: 06.709 16.158 bis 18.055/com.example.administrator.crmtestapp E/Volley: [341] BasicNetwork. performRequest: Unerwarteter Antwortcode 400 für http://103.75.33.98/BPService/GetAllBPService.svc/SetAllBP 05-25 10: 23: 06.710 16158-16158/com.beispiel.administrator.crmtestapp E/LOG_VOLLEY: com.android.volley.ServerError 05-25 10: 26: 47.326 21337-21337/? E/cutils-trace: Fehler beim Öffnen der Trace-Datei: Berechtigung verweigert (13) */Ich erhalte Volley 400 Fehler

+0

überprüfen Sie api Faust, Server-Seite –

+0

400 Bad Request. Der Server kann die Anfrage nicht verarbeiten, um ein Clientfehler – Lingeshwaran

+0

zu sein, dann erwartet Ihr Server einige andere Daten von Ihnen. Wir haben keine API-Dokumentation für Ihren Server, überprüfen Sie sie zuerst. –

Antwort

0

Postbote verwenden und prüfen. https://www.getpostman.com/ Antwort.

+0

Ich habe 200, wenn ich hart codierte Zeichenfolge als Parameter eingegeben habe, aber wenn ich versuche, Eingabe von Text bearbeiten, gibt es mir den gleichen Fehler. –

+0

Sie meinen, Sie müssen Daten per POST-Anfrage mit gegebenen Daten an den Server senden. ?? –

+0

Ja, ich muss Daten auf dem Server senden –

0

Probieren Sie die Anfrage auf diese Weise zu senden:

RequestQueue requestQueue = Volley.newRequestQueue(BP_Details.this); 
StringRequest strReq = new StringRequest(Request.Method.POST, 
      URL, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      Log.i("response", response); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e(TAG, "Login Error: " + error.getMessage()); 
      Toast.makeText(getApplicationContext(), 
        error.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    }){ 

     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      Log.e(TAG,"Inside getParams"); 

      // Posting parameters to url 
      Map<String, String> jsonBody = new HashMap<>(); 
      jsonBody.put("COMPANY_NO",s); 
      jsonBody.put("LOCATION_NO",s1); 
      jsonBody.put("BP_CODE",code); 
      jsonBody.put("NAME",name); 
      jsonBody.put("ADDRESS1",address1); 
      jsonBody.put("ADDRESS2",address2); 
      jsonBody.put("CITY",city); 
      jsonBody.put("STATE",state); 
      jsonBody.put("COUNTRY",country); 
      jsonBody.put("ZIP",zip); 
      jsonBody.put("AREA_CODE",area); 
      jsonBody.put("EMAIL",email); 
      jsonBody.put("CURRENCY",currency); 
      jsonBody.put("TIN_GRN",tin); 
      jsonBody.put("CST_NO",cst); 
      jsonBody.put("PAN_NO",pan); 
      jsonBody.put("SALES_PERSON_NO",salesPname); 
      jsonBody.put("PHONE_NO",mobile); 
      jsonBody.put("LOCATION",cLocation); 

      return params; 
     } 
    }; 
    // Adding request to request queue 
    queue.add(strReq);