简体   繁体   English

执行时出现Android AsyncTask错误

[英]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: 我认为您在这里有两个明显的问题:

  1. delegate is accessed from several threads, without synchronization. 从多个线程访问delegate ,而无需同步。 That is incorrect. 那是不对的。
  2. you call 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 ,也没有完整的错误消息。

Look at this question and my answer I believe that it'll help you a lot. 看看这个问题和我的回答,我相信它将对您有很大帮助。 Since the AsyncTask is no longer used for http request. 由于AsyncTask不再用于http请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM