Advertisement
Guest User

jd24dev5

a guest
Jun 7th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. private class DownloadUserInfoTask extends AsyncTask<String, Void, Void> {
  2.  
  3.         int mResponseCode;
  4.         String mResultString;
  5.         HttpsURLConnection urlConnection;
  6.  
  7.         @Override
  8.         protected Void doInBackground(String... args){
  9.  
  10.             String username = args[0];
  11.  
  12.             String encodedUserName = "";
  13.  
  14.  
  15.  
  16.             try{
  17.                 encodedUserName = URLEncoder.encode(username,"utf-8");
  18.             } catch (UnsupportedEncodingException e1){
  19.                 // TODO Auto-generated catch block
  20.                 e1.printStackTrace();
  21.             }
  22.  
  23.             String fetchUrl = "https://jsonplaceholder.typicode.com/albums/" + encodedUserName;
  24.             URL url = null;
  25.             try {
  26.                 url = new URL(fetchUrl);
  27.             } catch (MalformedURLException e) {
  28.                 e.printStackTrace();
  29.             }
  30.  
  31.             try {
  32.                 urlConnection = (HttpsURLConnection) url.openConnection();
  33.  
  34.  
  35.                 mResponseCode = urlConnection.getResponseCode();
  36.  
  37.                 if(mResponseCode == 200){
  38.                     mResultString = urlConnection.getResponseMessage();
  39.  
  40.                 }
  41.             } catch (IOException e) {
  42.                 e.printStackTrace();
  43.             }
  44.  
  45.  
  46.             return null;
  47.         }
  48.  
  49.         @Override
  50.         protected void onPostExecute(Void arg){
  51.             mProgressDialog.dismiss();
  52.             if(mResponseCode == 200){
  53.                 String inputLine;
  54.                 StringBuilder response = new StringBuilder();
  55.                 BufferedReader in = null;
  56.  
  57.                 int c;
  58.  
  59.                 try {
  60.  
  61.                     BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  62.  
  63.                     while ((inputLine = br.readLine()) != null){
  64.                         Toast.makeText(MainActivity.this,inputLine,Toast.LENGTH_LONG).show();
  65.                         response.append(inputLine + "\n");
  66.  
  67.                     }
  68.        /*             while ((c = br.read()) != -1){
  69.  
  70.                         response.append((char) c);
  71.  
  72.                     }
  73.                     */
  74.          //           String t = response.toString();
  75.          //           br.close();
  76.           //          urlConnection.disconnect();
  77.  
  78.  
  79.                 } catch (IOException e) {
  80.                     e.printStackTrace();
  81.                 }
  82.           //      Toast.makeText(MainActivity.this,response.toString(),Toast.LENGTH_SHORT).show();
  83.                // mTxtvUserInfo.setText(response.toString());
  84.             }
  85.             else{
  86.                 Toast.makeText(MainActivity.this,"Gegevens konden niet worden opgehaald",Toast.LENGTH_LONG).show();
  87.             }
  88.             Toast.makeText(MainActivity.this,"Done",Toast.LENGTH_LONG).show();
  89.         }
  90.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement