显然我在这里遗漏了一些东西——我已经阅读了关于 AsyncTasks 和线程的 developer.android 参考和指南材料——加上这里的大量帖子,这也不是我的第一个牛仔竞技表演。我有一个内部 AsyncTask 类,它从 url 中获取 JSON 并解析它。我一直遇到访问问题,似乎无法获得正确的组合。这是主要公共类中的一段代码,即“消费者”。我可以添加更多代码,但我相信这将证明需要什么。蒂亚!
// 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> {
这是:
Consumer.JSONParser 类型必须实现继承的抽象方法 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