gpt4 book ai didi

android - onPostExecute(String) 方法从不在本地使用 - AsyncTask

转载 作者:太空狗 更新时间:2023-10-29 16:18:18 24 4
gpt4 key购买 nike

我正在开发一个小程序,它只是从服务器检索一些 JSON,解析它,然后将值返回到屏幕。我在 AsyncTask 中执行所有这些操作,但是 onPostExecute(String result) 给出以下警告:MainActivity.RESTfulGET 类型的方法 onPostExecute(String) 从未在本地使用

我不确定为什么会收到此警告,并且我已经通过 Google 上的许多不同来源尝试找到此问题的答案,不幸的是我一直无法找到任何解决方案。

这是我的代码(请注意,解析器尚未完成,但我不需要任何帮助):

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private RESTfulGET mTask;

Button bParseJSON;

TextView tvJSON;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Class.forName("android.os.AsyncTask");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bParseJSON = (Button) findViewById(R.id.bParseJSON);

tvJSON = (TextView) findViewById(R.id.tvJSON);

bParseJSON.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mTask = (RESTfulGET) new RESTfulGET().execute("http://www.seasite.niu.edu/cs428_628/Assignments/A2_Client_list_json.txt");
mTask.getStatus();
}
});

}

private class RESTfulGET extends AsyncTask <String, Integer, String> {
HttpClient httpClient;
HttpGet httpGet;

@Override
protected String doInBackground(String... urls) {
String returnValue = null;

HttpResponse serverResponse;

String url = urls[0];
httpClient = new DefaultHttpClient(); // Optional args to alter default settings
httpGet = new HttpGet(url); // or the passed-in URI argument string

try {
serverResponse = httpClient.execute(httpGet);
if(serverResponse != null &&
serverResponse.getStatusLine().getStatusCode() == 200) {
// Success, do something
HttpEntity responseEntity = serverResponse.getEntity();

InputStream is = null;

try {
is = responseEntity.getContent();
BufferedReader readBuf = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

char[] buf = new char[1024]; // an initial size
int bytesRead;

// this could block; thus it is done in a background thread
while((bytesRead = readBuf.read(buf, 0, 1024)) != -1) {
sb.append(buf, 0, bytesRead);
}

returnValue = sb.toString();
}
finally {
// do nothing
}
}
} catch(IOException e) {

} // try

return returnValue;
} // doInBackground

@Override
protected void onProgressUpdate(Integer... progress) {

}

protected void onPostExcute(String result) {
// back on main thread, get the result passed by doInBackground() and cast it in preparation to parsing
if(result != null) {
try {
JSONObject json = new JSONObject(result);
JSONArray jArray = ((JSONObject) json).getJSONArray("clients");

for(int i = 0; i < jArray.length(); i++) {
JSONObject client_json = jArray.getJSONObject(i);
String name = client_json.getString("name");
String profession = client_json.getString("profession");
String dob = client_json.getString("dob");
Log.d("RESULT", "Name: " + name);
Log.d("RESULT", "Profession: " + profession);
Log.d("RESULT", "DOB: " + dob);
} // for
} catch (JSONException e) {
e.printStackTrace();
}
} // if
} // onPostExecute

} // RESTfulGET
}

最佳答案

这里有一个错字:

  protected void onPostExcute(String result) {

应该是:

  @Override
protected void onPostExecute(String result) {

所以由于 onPostExcute 中的拼写错误(e 丢失),您最终编写了自己的函数,并且没有警告您没有覆盖任何方法,因为您也没有@Override注解

关于android - onPostExecute(String) 方法从不在本地使用 - AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21839340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com