0

I am making a app that requires login auth. And i have api:

curl --user username:password -X GET -http://sample.com

Now i want to auth the login and get the response.

I have look into this but am not getting any result.

I have done this. But it keep going on the catch block.

public static String getRequest() {
    StringBuffer stringBuffer = new StringBuffer("");
    BufferedReader bufferedReader = null;
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet();

        URI uri = new URI("http://sample.com");
        httpGet.setURI(uri);
        httpGet.addHeader(BasicScheme.authenticate(
                new UsernamePasswordCredentials("username", "password"),
                HTTP.UTF_8, false));

        HttpResponse httpResponse = httpClient.execute(httpGet);
        InputStream inputStream = httpResponse.getEntity().getContent();
        bufferedReader = new BufferedReader(new InputStreamReader(
                inputStream));

        String readLine = bufferedReader.readLine();
        while (readLine != null) {
            stringBuffer.append(readLine);
            stringBuffer.append("\n");
            readLine = bufferedReader.readLine();
        }
    } catch (Exception e) {
        // TODO: handle exception
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                // TODO: handle exception
            }
        }
    }
    return stringBuffer.toString();
}

How can i accomplish this?

Thanks in Advance.

Community
  • 1
  • 1
Sangharsha
  • 88
  • 11

2 Answers2

1

You call network operation on main thread. This case crash. Use volley library or use your code in AsyncTask or thread. Remember to update your ui in main thread.

Adam Miśtal
  • 715
  • 4
  • 15
0

Consider using an Android Asynchronous Http Client. That'll simplify life for you.

Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54