时的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文件。
如果有另一种解决方案和更好的请与大家共享。
英雄联盟工作感谢的人:) – medo
@medo大。如果有帮助,你能否接受这篇文章作为答案? –
确定我接受,但你知道必须等待5分钟才能接受:D – medo