0

I am using volley library but when I use basic authentication then I do not got any response and also volley is not showing any error. I am also try HttpUrlConnection with Asyntask but I face same problem. Please provide solution this problem.

I also hit my api via Postman client, ARC and my api is working fine with these client.

MyJsonObjectRequest.java

public class MyJsonObjectRequest extends JsonRequest<JSONObject> {
    private Priority priority = null;
    //private Map<String, String> headers = new HashMap<String, String>();

    public MyJsonObjectRequest(int method, String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener);

    }

    public MyJsonObjectRequest(String url, JSONObject jsonRequest, Listener<JSONObject> listener,
                               ErrorListener errorListener) {
        this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest,
                listener, errorListener);
    }

    @Override
    public Priority getPriority() {
        if (this.priority != null) {
            return priority;
        } else {
            return Priority.NORMAL;
        }
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        //Log.e("JSON"," AUTH CODE "+createBasicAuthHeader("MaS", "123456"));
        return createBasicAuthHeader("username", "123456");
    }

    Map<String, String> createBasicAuthHeader(String username, String password) {
        Map<String, String> headerMap = new HashMap<String, String>();
        String credentials = username + ":" + password;
        String encodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
        headerMap.put("Content-Type", "application/json; charset=utf-8");
        headerMap.put("Authorization", "Basic " + encodedCredentials);

        return headerMap;
    }

    public void setHeader(String title, String content) {
       // headers.put(title, content);
    }

    public void setPriority(Priority priority) {
        this.priority = priority;
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    protected VolleyError parseNetworkError(VolleyError volleyError){

        NetworkResponse response = volleyError.networkResponse;

        if (response!=null)
        {
            Log.e("TAG"," VOLLEY ERROR1 "+response.statusCode);
        }

        if(volleyError.networkResponse != null && volleyError.networkResponse.data != null){
            VolleyError error = new VolleyError(new String(volleyError.networkResponse.data));

            Log.e("TAG"," VOLLEY ERROR "+error.toString());
            volleyError = error;
        }

        return volleyError;
    }
}


My MainActvity

public class JsonObjectActivity extends AppCompatActivity {

    JsonObjectRequest gsonRequest;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json_object);
        textView = (TextView) findViewById(R.id.tv2);
        try {
            doingProcessJsonArray();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void doingProcessJsonArray() throws JSONException {




           // i am use local server like
        String newUrl="http://myserver:8023/Users.svc/password_encrypt";



        JSONObject params = new JSONObject();
        params.put("PASSWORD", "12345655");
        //params.put("domain", "http://samset.info");
        gsonRequest =new JsonObjectRequest(Request.Method.POST,newUrl, params, successListener(), errorListener());
        Constants.showProgressDialog(this, "Loading..");
        //gsonRequest.setRetryPolicy(new DefaultRetryPolicy(6000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        // Start progressbar

       // gsonRequest.setPriority(Request.Priority.IMMEDIATE);

        Log.e("TAG"," request "+gsonRequest.toString());
        VolleySingleton.getInstance(this).addToRequestQueue(gsonRequest, "volleyrequest");
    }

    private Response.ErrorListener errorListener() {
        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Volley","Error "+error.toString());
            }
        };
    }

    public Response.Listener successListener()
    {
        return new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject response) {
                Constants.dismissDialog();
                Log.e("Volley","Responce "+response.toString());
                textView.setText("JSONObject RESPONCE > "+response.toString());
            }
        };

    }


}

postman screen shot

Samset
  • 109
  • 7
  • Try `headerMap.put("Authorization", encodedCredentials);` instead. If it's not solved, pls post your Postman screenshot – BNK Oct 13 '16 at 06:09
  • Where are you running your app?? in the emulator or Real device? – Nandhu Oct 13 '16 at 06:14
  • i am using real device and i update my question with postman screen shot – Samset Oct 13 '16 at 07:26
  • I think the reason is your `successListener()` and `errorListener()`, please read http://stackoverflow.com/questions/32672942/android-volley-library-do-we-always-have-to-repeat-response-listener-and-respon/32674056#32674056 for more information – BNK Oct 13 '16 at 07:36

0 Answers0