当我在搜索编辑框中输入数据并单击数据时,它会给出json数据,第一次给出结果,但第二次单击搜索编辑框时,它显示AsyncTask错误。。请帮忙解决这个问题。。提前感谢
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
JSONObject jsonobject;
ListView listview;
ListViewAdapter adapter;
EditText search;
ArrayList<HashMap<String, String>> arraylist;
static String name = "name";
static String temp = "temp";
static String pressure = "pressure";
DownloadJSON Asynctask= new DownloadJSON();
CityJson cityJson=new CityJson();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search= (EditText) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
String zipcode = search.getText().toString();
final String []params=
{"http://api.openweathermap.org/data/2.5/weather?q="+zipcode+",in&appid=2de143494c0b295cca9337e1e96b00e0"};
cityJson.execute(params);
}
});
Asynctask.execute();
}
private class CityJson extends AsyncTask<String,Void,Void>
{
@Override
protected Void doInBackground(String... params)
{
try
{
arraylist = new ArrayList<HashMap<String, String>>();
jsonobject = CityAsyncTask
.getJSONfromURL(params[0]);
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", jsonobject.getString("name"));
JSONObject mainobJsonObject = jsonobject.getJSONObject("main");
map.put("temp", mainobJsonObject.getString("temp"));
map.put("pressure", mainobJsonObject.getString("pressure"));
arraylist.add(map);
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args)
{
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
}
}
private class DownloadJSON extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void...params) {
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
try {
String [] weatherurl={"http://api.openweathermap.org/data/2.5/weather?zip=560086,in&appid=2de143494c0b295cca9337e1e96b00e0",
"http://api.openweathermap.org/data/2.5/weather?q=chennai,in&appid=2de143494c0b295cca9337e1e96b00e0",
"http://api.openweathermap.org/data/2.5/weather?q=delhi,in&appid=2de143494c0b295cca9337e1e96b00e0",
"http://api.openweathermap.org/data/2.5/weather?q=mumbai,in&appid=2de143494c0b295cca9337e1e96b00e0",
"http://api.openweathermap.org/data/2.5/weather?q=kolkata,in&appid=2de143494c0b295cca9337e1e96b00e0"};
for (int i = 0; i < weatherurl.length; i++) {
jsonobject = CityAsyncTask
.getJSONfromURL(weatherurl[i]);
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", jsonobject.getString("name"));
JSONObject mainobJsonObject = jsonobject.getJSONObject("main");
map.put("temp", mainobJsonObject.getString("temp"));
map.put("pressure", mainobJsonObject.getString("pressure"));
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
}
}
}
您只能执行一次特定的异步任务。您需要创建一个新的异步任务实例才能再次执行。
有关详细信息,请阅读AsyncTask | Android Developers中的线程规则。
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
String zipcode = search.getText().toString();
final String []params=
{"http://api.openweathermap.org/data/2.5/weather?q="+zipcode+",in&appid=2de143494c0b295cca9337e1e96b00e0"};
//cityJson.execute(params);
//Replace above line with following line
cityJson = new CityJson();
cityJson.execute(params);
}
});