Json AsyncTask - 无需解析即可获取页面内容

时间:2016-12-12 19:45:15

标签: java android json android-asynctask

我是java的新手,我无法理解问题是什么......我只想获得页面{"base":"USD","date":"2016-12-12","rates":{"RON":4.2538}}的内容而不解析它。

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);

        }
    }
}

我得到的东西&#34; audtext&#34;

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

我在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

由于

1 个答案:

答案 0 :(得分:2)

  1. 您对网址的原始使用看起来是正确的,请勿对其进行编码。我不确定为什么找不到该URL。您是否可以在运行代码的确切设备上使用浏览器(即使是模拟器)来浏览到该URL?其他网址,例如https://google.com/
  2. INSERT INTO Bookings (user_type,departing, arriving, departure_date, departure_time, arrival_time, price) SELECT 'manager',depart_from, arrive_in, travel_date, departure_time, arrival_time, price FROM train WHERE (train_ID = @p_ID_choice); 的返回值不是您所需要的。而是使用.execute()中传递给您的字符串。
  3. e.g

    onPostExecute

    read more about AsyncTask