0

I'm new to java and i can't understand what's the problem... I just wanna get just the content of the page {"base":"USD","date":"2016-12-12","rates":{"RON":4.2538}} without parsing it.

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private TextView audtext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        audtext = (TextView) findViewById(R.id.dataAUD);

        String aud2ron = new GetContentJson().execute("AUD","RON").toString();

        audtext.setText(aud2ron);
    }
    class GetContentJson extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection connection = null;
            InputStream inputStream = null;

            try {
                connection = (HttpURLConnection) (new URL("https://api.fixer.io/latest?base="+params[0]+"&symbols="+params[1])).openConnection();
                connection.setRequestMethod("GET");
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.connect();

                //Read the resp
                StringBuffer stringBufferr = new StringBuffer();
                inputStream = connection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String line = null;

                while((line = bufferedReader.readLine())!= null)
                {
                    stringBufferr.append(line+"/r/n");
                }

                inputStream.close();
                connection.disconnect();

                return stringBufferr.toString();

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

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

        }
    }
}

The thing I get in "audtext"

"companyname"."appname".MainActivity $GetContentJson@"Random Hex code"

Error that i get in CatLog:

    12-12 21:32:17.204 10609-10609/? I/art: Not late-enabling -Xcheck:jni (already on)
12-12 21:32:17.222 10609-10617/? E/art: Failed writing handshake bytes (-1 of 14): Broken pipe
12-12 21:32:17.223 10609-10617/? I/art: Debugger is no longer active
12-12 21:32:17.235 10609-10609/? I/InstantRun: Instant Run Runtime started. Android package is qunetzapps.cursvalutar, real application class is null.
12-12 21:32:17.728 10609-10609/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-12 21:32:17.841 10609-10629/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                   [ 12-12 21:32:17.842 10609:10609 D/         ]
                                                   HostConnection::get() New Host Connection established 0x7f2139166bc0, tid 10609
12-12 21:32:17.844 10609-10609/? D/Atlas: Validating map...
12-12 21:32:17.878 10609-10629/? I/OpenGLRenderer: Initialized EGL, version 1.4
12-12 21:32:17.893 10609-10629/? D/OpenGLRenderer: Enabling debug mode 0
12-12 21:32:17.900 10609-10629/? W/EGL_emulation: eglSurfaceAttrib not implemented
12-12 21:32:17.900 10609-10629/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f21390df680, error=EGL_SUCCESS
12-12 21:32:18.017 10609-10626/? W/System.err: java.io.FileNotFoundException: https://api.fixer.io/latest?base=AUD&symbols=RON
12-12 21:32:18.017 10609-10626/? W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
12-12 21:32:18.017 10609-10626/? W/System.err:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
12-12 21:32:18.017 10609-10626/? W/System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
12-12 21:32:18.017 10609-10626/? W/System.err:     at qunetzapps.cursvalutar.MainActivity$GetContentJson.doInBackground(MainActivity.java:47)
12-12 21:32:18.018 10609-10626/? W/System.err:     at qunetzapps.cursvalutar.MainActivity$GetContentJson.doInBackground(MainActivity.java:31)
12-12 21:32:18.018 10609-10626/? W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
12-12 21:32:18.018 10609-10626/? W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-12 21:32:18.018 10609-10626/? W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-12 21:32:18.018 10609-10626/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-12 21:32:18.018 10609-10626/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-12 21:32:18.018 10609-10626/? W/System.err:     at java.lang.Thread.run(Thread.java:818)
12-12 21:32:18.589 10609-10629/qunetzapps.cursvalutar W/EGL_emulation: eglSurfaceAttrib not implemented
12-12 21:32:18.589 10609-10629/qunetzapps.cursvalutar W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f2139246a40, error=EGL_SUCCESS
12-12 21:32:18.613 10609-10629/qunetzapps.cursvalutar V/RenderScript: 0x7f2139252000 Launching thread(s), CPUs 2

Thanks

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
No Name
  • 11
  • 1
  • Hi, did you realize you are connecting fine and then you can't get the page you are looking for (The FileNotFoundException)? , did you test on the browser and validated the URL is properly set? – sup4eli Dec 12 '16 at 20:00
  • the URL is proprely working, you can try for yourself: https://api.fixer.io/latest?base=AUD&symbols=RON – No Name Dec 12 '16 at 20:03
  • You're right. Did you try using URLEncoder as suggested here: http://stackoverflow.com/questions/26649540/filenotfoundexception-at-url ? – sup4eli Dec 12 '16 at 20:09
  • see my answer, the "FileNotFoundException" disappeared but i still get some errors. – No Name Dec 12 '16 at 20:19
  • Is there any reason you aren't using Volley? So much simpler than AsyncTask – OneCricketeer Dec 12 '16 at 20:35
  • I might recommend following this answer I have. It's generally how I use AsyncTasks for just reading strings from a URL. http://stackoverflow.com/a/35210468/2308683 Obviously, replace (username, password) with your two currency symbols. – OneCricketeer Dec 12 '16 at 20:54

1 Answers1

2
  1. Your original use of the URL looks correct, don't encode it or anything. I'm not sure why the URL isn't found. Can you use the browser on the exact device you're running the code on (even if it's the emulator) to browse to that URL? To some other URL such as https://google.com/?
  2. The return value of .execute() is not what you need. Instead use the string passed to you in onPostExecute.

e.g

@Override
protected void onPostExecute(String s) {
  audText.setText(s);
}

Also read more about AsyncTask.

orip
  • 73,323
  • 21
  • 116
  • 148