gpt4 book ai didi

java - Json AsyncTask - 获取页面内容而不解析

转载 作者:行者123 更新时间:2023-12-02 03:10:32 25 4
gpt4 key购买 nike

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

}
}
}

我在“audtext”中得到的东西

"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. 您最初使用的网址看起来是正确的,请勿对其进行编码或任何其他操作。我不确定为什么找不到该 URL。您可以使用正在运行代码的确切设备(即使是模拟器)上的浏览器来浏览到该 URL 吗?到其他一些 URL,例如 https://google.com/
  2. .execute() 的返回值不是您需要的。而是使用在 onPostExecute 中传递给您的字符串。

例如

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

还有read more about AsyncTask .

关于java - Json AsyncTask - 获取页面内容而不解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41108362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com