执行

2016-08-20 31 views
0

时的Android的AsyncTask错误这是我的同班同学,从一个网址,以获取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(); 
} 


} 

在我actiivty我这样称呼它:

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(); 

但其将所有的红色,并显示:

不相容的典型ES

必需的JSON类

发现Anroid.os.asyncTask java.lang.Void的,java.lang.Void的,org.json.JSONObject

我有问题上的AsyncTask任何帮助,还我不知道如果它的好主意得到json文件。

如果有另一种解决方案和更好的请与大家共享。

回答

1

的AsyncTask.execute的返回类型()方法是始终的AsyncTask类型,而不是你的子类JSON - 这就是为什么你得到你的错误。

所以应该

AsyncTask<Void, Void, JSONObject> fs = new Json(s, new Json.AsyncResponse(){ 
... 
+0

英雄联盟工作感谢的人:) – medo

+0

@medo大。如果有帮助,你能否接受这篇文章作为答案? –

+0

确定我接受,但你知道必须等待5分钟才能接受:D – medo

-1

this的问题,我的答案我相信,它会帮助你很多。由于AsyncTask不再用于http请求。

0

我认为你有两个明显的问题,在这里:

  1. delegate从多个线程访问,而无需同步。这是不正确的。
  2. onPreExecute调用delegate.processFinished。我想你打算把它调用onPostExecute

对于Incompatible Type错误,这是很难说的,因为既没有在任何上市类,名为MyJson,也没有完整的错误消息。