我的异步功能是:
class ProfileCategoryCall extends AsyncTask<String, String, String>{
public ProfileCategoryCall() {
// TODO Auto-generated constructor stub
}
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
JSONObject HomeCardString = new JSONObject();
JSONObject ABC = new JSONObject();
try {
responseString = result;
joiningYear = new JSONObject(responseString);
HomeCardString = joiningYear.getJSONObject("CategoryLikesResult");
ABC = HomeCardString.getJSONObject("CategoryLikesResult");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Gson gson = new Gson();
try
{
((ProfileActivity)getActivity()).interestResult = gson.fromJson(ABC.toString(), LikeCategorySummaryResult.class);
}
catch(Exception e)
{
Log.i("myyyy", e.getMessage());
e.printStackTrace();
}
} catch (Throwable t) {
Log.e("My App", "Could not parse malformed JSON: \"" + responseString + "\"" + t.getMessage());
}
PopulateData();
progress.setVisibility(View.GONE);
super.onPostExecute(result);
//Do anything with response..
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progress.setVisibility(View.VISIBLE);
super.onPreExecute();
}
}
在调用异步服务时调用此错误。
08-28 11:15:38.140: E/AndroidRuntime(14310): FATAL EXCEPTION: AsyncTask #1
08-28 11:15:38.140: E/AndroidRuntime(14310): java.lang.RuntimeException: An error occured while executing doInBackground()
08-28 11:15:38.140: E/AndroidRuntime(14310): at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-28 11:15:38.140: E/AndroidRuntime(14310): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-28 11:15:38.140: E/AndroidRuntime(14310): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-28 11:15:38.140: E/AndroidRuntime(14310): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-28 11:15:38.140: E/AndroidRuntime(14310): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-28 11:15:38.140: E/AndroidRuntime(14310): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-28 11:15:38.140: E/AndroidRuntime(14310): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-28 11:15:38.140: E/AndroidRuntime(14310): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-28 11:15:38.140: E/AndroidRuntime(14310): at java.lang.Thread.run(Thread.java:856)
08-28 11:15:38.140: E/AndroidRuntime(14310): Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 355: http://example.net/Service.svc/android/Profile/1361769105/Like/EXAMPLE?category=Public
08-28 11:15:38.140: E/AndroidRuntime(14310): at java.net.URI.create(URI.java:727)
08-28 11:15:38.140: E/AndroidRuntime(14310): at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
08-28 11:15:38.140: E/AndroidRuntime(14310): at com.stalker.androidapp.InterestTabFragment$ProfileCategoryCall.doInBackground(InterestTabFragment.java:308)
08-28 11:15:38.140: E/AndroidRuntime(14310): at com.stalker.androidapp.InterestTabFragment$ProfileCategoryCall.doInBackground(InterestTabFragment.java:1)
08-28 11:15:38.140: E/AndroidRuntime(14310): at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-28 11:15:38.140: E/AndroidRuntime(14310): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-28 11:15:38.140: E/AndroidRuntime(14310): ... 5 more
我已经看到了其他类似的问题,但答案是删除使用doInBackground()中的UI的代码,我不认为我有任何不必要的代码。所以请告诉我,如果我错了。