[英]Android AsyncTask error when execute
This is my class to get Json from an url : 这是我的从URL获取Json的课程:
public class Json extends AsyncTask<Void , Void, JSONObject > {
String theUrl;
public AsyncResponse delegate = null;
InputStream is = null;
String result = "";
JSONObject jsonObject = null;
StringBuilder stringBuilder;
URLConnection urlConnection;
BufferedReader reader;
public Json(String theUrl , AsyncResponse delegate) {
this.theUrl = theUrl;
this.delegate = delegate;
}
public interface AsyncResponse {
void processFinish(JSONObject output);
}
@Override
protected JSONObject doInBackground(Void... params) {
try {
URL url = new URL(theUrl);
urlConnection = url.openConnection();
} catch(Exception e) {
Log.d("Fff22", " " +e);
return null;
}
// Read response to string
try {
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
} catch(Exception e) {
return null;
}
// Convert string to object
try {
jsonObject = new JSONObject(result);
} catch(JSONException e) {
return null;
}
return jsonObject;
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
delegate.processFinish(jsonObject);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onCancelled(JSONObject jsonObject) {
super.onCancelled(jsonObject);
}
@Override
protected void onCancelled() {
super.onCancelled();
}
}
And in my actiivty I call it like this : 在我的活动中,我这样称呼它:
String s = "http://192.168.0.106/json/259616167dfea1870e4d0330caa0323b.json";
Json fs = new Json(s, new Json.AsyncResponse(){
@Override
public void processFinish(JSONObject output) {
Log.d("fwwwwwww "," "+output);
}
}).execute();
But its be all on red color and show : 但是它全是红色并显示:
incompatible types 不兼容的类型
required Json class 必修课程
found Anroid.os.asyncTask java.lang.void , java.lang.void , org.json.JSONObject 找到Anroid.os.asyncTask java.lang.void,java.lang.void,org.json.JSONObject
I have problem on asyncTask any help also I dont know if its good idea to get json file. 我对asyncTask有任何帮助的问题,我也不知道获取json文件是否是个好主意。
If there are another solution and its better please share it. 如果还有其他解决方案,更好的方法是与他人分享。
The return type of the AsyncTask.execute() method is always AsyncTask type, not your child class Json - that's why you get your error. AsyncTask.execute()方法的返回类型始终是AsyncTask类型,而不是您的子类Json-这就是为什么会出现错误的原因。
So it should be 所以应该
AsyncTask<Void, Void, JSONObject> fs = new Json(s, new Json.AsyncResponse(){
...
I think you have two obvious problems, here: 我认为您在这里有两个明显的问题:
delegate
is accessed from several threads, without synchronization. delegate
,而无需同步。 That is incorrect. delegate.processFinished
in onPreExecute
. onPreExecute
调用delegate.processFinished
onPreExecute
。 I think you intend to call it in onPostExecute
onPostExecute
调用它 As for the Incompatible Type
error, it is hard to tell, because there is neither any class in your listing, called MyJson
, nor is there a complete error message. 至于
Incompatible Type
错误,很难说,因为清单中没有任何类称为MyJson
,也没有完整的错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.