0

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

shivaji
  • 1
  • 3

1 Answers1

0

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
  • My application is live on store and I only need to use basic auth, as there are 40+ webservice calls in app. – shivaji Feb 20 '18 at 05:13
  • @shivaji, httpconections are part of libraries which android keeps improving in forecoming versions. You should use backword compatibility with updated methodology and it gets bundled with it and works on previous versions too. Also you should stay away from using depreciated libraries –  Feb 20 '18 at 05:19
  • Do you have solution regarding my code that I have added ? – shivaji Feb 20 '18 at 05:23
  • Yes => Using google volley only. even if 40+ calls you need not make 40+ times coding, you can make it with single public function. You have to just delete earlier request codes with call. and define a function which will have above 10 lines and a dependancy jar of volly. Hardly takes a 1 hour –  Feb 20 '18 at 05:36
  • No=> Google keeps revisioning its packages, classes, functions, If you want the solution in the same code.., I can not help in this. –  Feb 20 '18 at 05:38