0

I have a Rest web service, also service has a Basic Authentication. When I connect to the server, which asking me for a username and password. After that, I access a web service return a user about JsonArray. (username, birthday, age e.g.)

My purpose returning data parse at show the Textviews. I want to use AsyncTask Http request.

Can you give some examples of it? Thanks.

2 Answers2

0
class RequestTask extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... uri) {
        try {
                postData(uri[0]);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return null;
    }
    @Override
    protected void onCancelled() {
        myTask2.cancel(true);

    }
    @Override
    protected void onPostExecute(String url) {
        LinearLayouts();
    }
}

public static String postData(String url) throws UnsupportedEncodingException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httppost = new HttpGet(url);
    String responseString = "";

    try {
        HttpResponse response = httpclient.execute(httppost);
        StatusLine statusLine = response.getStatusLine();
        InputStream inputStream = null;
        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
            inputStream = response.getEntity().getContent();
            if(inputStream != null)
                responseString = convertInputStreamToString(inputStream);
            else
                responseString = "Did not work!";
            JSONObject reader = null;
            JSONArray sys  = null;
            try {
                reader = new JSONObject(responseString);
                String res_metar = reader.getString("YOUR TAG");

            } catch (JSONException e) {
                e.printStackTrace();
            }
            //a = responseString;

        } else{
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (ClientProtocolException e) {

    } catch (IOException e) {

        // process execption
    }
    return responseString;
}

private static String convertInputStreamToString(InputStream inputStream) throws IOException{
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

}

You should write it to top in your class.

private static RequestTask myTask2;

and add it to your mainactivity,

myTask2=(RequestTask)new RequestTask().execute(url);
alican akyol
  • 303
  • 3
  • 14
0

alican has already mentioned about asynctask.

Have a look at Error "HTTP/1.1 401 Unauthorized" with basic authentication in android - EWS 2010 which contains information needed for basic authentication

Community
  • 1
  • 1
  • Ok, I understood used to AsyncTask however I didn't know how to use Basic Authentication, example is mixed. – Tunahan Tolga Yıldız Oct 13 '15 at 05:36
  • Following lines were there in above link : UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, pass); Header header = new BasicScheme().authenticate(credentials, mHttpRequest); mHttpRequest.addHeader(header); – Digish Gabhawala Oct 13 '15 at 06:13