gpt4 book ai didi

android - 另一个 AsyncTask 问题 - 内部类看不到 doInBackground 方法

转载 作者:行者123 更新时间:2023-11-29 21:52:25 25 4
gpt4 key购买 nike

显然我在这里遗漏了一些东西 - 我已经阅读了关于 AsyncTasks 和线程的 developer.android 引用和指南 Material - 加上这里的许多帖子,这也不是我的第一个牛仔竞技表演。我有一个内部 AsyncTask 类,它从 url 中获取 JSON 并解析它。我一直遇到访问问题,似乎无法获得正确的组合。这是主要公共(public)类“Consumer”中途的一段代码。我可以添加更多代码,但我相信这将证明需要什么。 TIA!

// some method
if(arg1==b){
b.setClickable(false);
a.setClickable(true);
// next, build the url
srchStr = urla + "&q=upc:" + value1 + urlc;
// execute the parser
new JSONParser().execute(value1);
}

// ...

static class JSONParser extends AsyncTask<String, Void, JSONObject> {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";


public JSONObject doInBackground(String url) { //getJSONFromUrl
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

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

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try to parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;
}
protected void onPostExecute(String result) {
//TextView tv2 = (TextView) findViewById(R.id.tv2);
tv2.setText("json"); // txt.setText(result);


}
}

在线上出现错误:

static class JSONParser extends AsyncTask<String, Void, JSONObject> {

即:

The type Consumer.JSONParser must implement the inherited abstract method AsyncTask.doInBackground(String...)

....然而,doInBackground 方法在那里......

我希望这些信息足以解决此问题。我希望这是我一直想念的简单的东西....

更新:我进行了建议的更改,因为它们当然有意义,但它现在可以编译,但不会运行。请再看一眼,如果我弄错了,或者这是另一个问题,请告诉我......这是更新后的代码:

if(arg1==b){
b.setClickable(false);
a.setClickable(true);
// next, build the url
srchStr = urla + "&q=upc:" + value1 + urlc;
// execute the parser
new JSONParser().execute(value1);
}
}

static class JSONParser extends AsyncTask<String, Void, JSONObject> {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";


public JSONObject doInBackground(String... urls) { //getJSONFromUrl
Consumer.srchStr = urls[0];
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(srchStr);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

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

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try to parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;
}
protected void onPostExecute(JSONObject result) {
//TextView tv2 = (TextView) findViewById(R.id.tv2);
result = jObj;
tv2.setText("result"); // txt.setText(result);
}
}
}

和 logcat 文本:

12-29 21:35:25.530: W/dalvikvm(2237): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
12-29 21:35:25.605: E/AndroidRuntime(2237): FATAL EXCEPTION: AsyncTask #1
12-29 21:35:25.605: E/AndroidRuntime(2237): java.lang.RuntimeException: An error occured while executing doInBackground()
12-29 21:35:25.605: E/AndroidRuntime(2237): at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-29 21:35:25.605: E/AndroidRuntime(2237): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-29 21:35:25.605: E/AndroidRuntime(2237): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-29 21:35:25.605: E/AndroidRuntime(2237): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-29 21:35:25.605: E/AndroidRuntime(2237): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-29 21:35:25.605: E/AndroidRuntime(2237): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-29 21:35:25.605: E/AndroidRuntime(2237): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-29 21:35:25.605: E/AndroidRuntime(2237): at java.lang.Thread.run(Thread.java:1096)
12-29 21:35:25.605: E/AndroidRuntime(2237): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters.
12-29 21:35:25.605: E/AndroidRuntime(2237): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:561)
12-29 21:35:25.605: E/AndroidRuntime(2237): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292)
12-29 21:35:25.605: E/AndroidRuntime(2237): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-29 21:35:25.605: E/AndroidRuntime(2237): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-29 21:35:25.605: E/AndroidRuntime(2237): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-29 21:35:25.605: E/AndroidRuntime(2237): at com.clicsys.pbuster.Consumer$JSONParser.doInBackground(Consumer.java:116)
12-29 21:35:25.605: E/AndroidRuntime(2237): at com.clicsys.pbuster.Consumer$JSONParser.doInBackground(Consumer.java:1)
12-29 21:35:25.605: E/AndroidRuntime(2237): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-29 21:35:25.605: E/AndroidRuntime(2237): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-29 21:35:25.605: E/AndroidRuntime(2237): ... 4 more

最佳答案

你需要改变你的方法签名

public JSONObject doInBackground(String... url)

这是因为doInBackground()接受无限数量的参数。要访问你想要的 String,你可以这样做

String currentUrl = url [0];

此外,您的 onPostExecute 需要一个 JSONObject 作为参数。这不会使用 ... 因为 onPostExecute() 只接受 1 个参数。

关于android - 另一个 AsyncTask 问题 - 内部类看不到 doInBackground 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14083988/

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