basic authentication server not working on jellybeans and kitkat

81 Views Asked by At

I am using basic authentic for http connection in app. App is working finr correctly on devices with higher versions. I have also searched for solution and It did not worked for me. Here is my code for connection

public static String executeHttpPost(Activity activity, String url,
                                     ArrayList<NameValuePair> postParameters) {
    String value = "{\"status\":false,\"message\":\"Server Timeout, connection problem, Please try later\"}";
    try {
        final String basicAuth = "Basic " + Base64.encodeToString(
                ("abc" + ":" + "abcd").getBytes(), Base64.NO_WRAP);
        networkConnection = new NetworkConnection();
        if (networkConnection.isOnline(activity)) {
            postParameters.add(new BasicNameValuePair("device_type","android"));
            HttpClient client = getNewHttpClient();
            HttpPost post = new HttpPost(url);

            try {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters, "UTF-8");


                post.setEntity(entity);

                post.setHeader("Authorization",basicAuth);
                post.setHeader("some-parameter","abc");
                org.apache.http.HttpResponse result = client.execute(post);
                value = EntityUtils.toString(result.getEntity());
            }catch (Exception e){}
            String s = "";
            for (NameValuePair param : postParameters) {
                s = s + param.getName() + " = " + param.getValue() + " ";
            }
            if (value != null) {
                WebUrl.ShowLog("From " + url +" parameters "+s
                        + " Response : " + value.trim());

                return value.trim();
            } else {

                return value;
            }
        } else {
            activity.startActivity(new Intent(activity, NoInternet.class));
            activity.finish();
            return "{\"status\":false,\"message\":\"\"}";

        }

    } catch (Exception e) {
        e.printStackTrace();

        return value;
    }
}

This is the only link I found, but it didn't work for me

1

There are 1 best solutions below

5
AudioBubble On

You should use Google Volley for the connections with the server. There are many ways to get connect, but using "Google Volley" in Android development is so simple, reliable and as it comes as a dependency it gets bundled with your package. So never worry about compatibility over many old and many current and upcoming Android versions.

I have used it 5 years ago and it was working on all major platforms. Very easy to program.

Have a look:

final TextView mTextView = (TextView) findViewById(R.id.text);
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>()
{
    @Override
    public void onResponse(String response)
    {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: "+ response.substring(0,500));
    }
}, new Response.ErrorListener() 
{
    @Override
    public void onErrorResponse(VolleyError error)
    {
        mTextView.setText("That didn't work!");
    }
});
// Add the request to the RequestQueue.
queue.add(stringRequest);

How simple is it.