There’s obviously something I’m missing here – I’ve been through the developer.android reference and guide material on AsyncTasks and threads – plus scores of posts here, and it’s not my first rodeo either.
I have an inner AsyncTask Class that grabs JSON from a url and parses it. I keep running into access issues and can’t seem to get the right combination. Here’s a chunk of code partway through the main public class, which is “Consumer”. I can ad more code, but I believe this will demonstrate what’s needed. 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);
}
}
The error comes up on the line:
static class JSONParser extends AsyncTask<String, Void, JSONObject> {
which is:
The type Consumer.JSONParser must implement the inherited abstract
method AsyncTask.doInBackground(String…)
….yet, the doInBackground method is there…..
I hope this is sufficient info to troubleshoot this. I expect it is something simple that I have been missing….
Update:
I made the recommended changes, as they certainly made sense, however it now compiles, but won’t run. Please take another look and let me know if I am mistaken, or if this is another issue…
Here’s the updated code:
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);
}
}
}
and the logcat text:
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
You need to change your method signature so it is
This is because
doInBackground()accepts in an infinite amount of arguments. To acess theStringyou want, you can then doAlso, your onPostExecute needs a
JSONObjectas the argument. This will not use the...becauseonPostExecute()only accepts in 1 argument.