我有 2 个活动用于从 .jsp 文件获取数据到 android 设备。我为此使用 GSON。我的项目包括 gson 库。问题是,当我启动活动时,应用程序失败了,并向我显示:不幸的是,应用程序停止了。
主要活动:
public class MainActivity extends Activity {
TextView capitalTextView;
ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
capitalTextView = (TextView) findViewById(R.id.capital_textview);
this.retrieveCapitals();
}
void retrieveCapitals() {
progressDialog = ProgressDialog.show(this,
"Please wait...", "Retrieving data...", true, true);
CapitalsRetrieverAsyncTask task = new CapitalsRetrieverAsyncTask();
task.execute();
progressDialog.setOnCancelListener(new CancelListener(task));
}
private class CapitalsRetrieverAsyncTask extends AsyncTask<Void, Void, Void> {
Response response;
@Override
protected Void doInBackground(Void... params) {
String url = "http://192.168.1.3:8080/Dochadzka/jsp/Android/GSON.jsp";
HttpGet getRequest = new HttpGet(url);
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse getResponse = httpClient.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
InputStream httpResponseStream = getResponseEntity.getContent();
Reader inputStreamReader = new InputStreamReader(httpResponseStream);
Gson gson = new Gson();
this.response = gson.fromJson(inputStreamReader, Response.class);
}
catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
StringBuilder builder = new StringBuilder();
for (Country country : this.response.data) {
builder.append(String.format("<br>Country: <b>%s</b><br>Capital: <b>%s</b><br><br>", country.getCountry(), country.getCapital()));
}
capitalTextView.setText(Html.fromHtml(builder.toString()));
progressDialog.cancel();
}
}
private class CancelListener implements OnCancelListener {
AsyncTask<?, ?, ?> cancellableTask;
public CancelListener(AsyncTask<?, ?, ?> task) {
cancellableTask = task;
}
@Override
public void onCancel(DialogInterface dialog) {
cancellableTask.cancel(true);
}
}
}
响应.java:
public class Response {
ArrayList<Country> data;
public Response() {
data = new ArrayList<Country>();
}
}
我的jsp文件是:
{"data":
[
{"country":"Angola", "capital": "Luanda"},
{"country":"Botswana", "capital": "Gaborone"},
{"country":"Democratic Republic of the Congo", "capital": "Kinshasa"},
{"country":"Lesotho", "capital": "Maseru"},
{"country":"Madagascar", "capital": "Antananarivo"},
{"country":"Malawi", "capital": "Lilongwe"},
{"country":"Mauritius", "capital": "Port Louis"},
{"country":"Mozambique", "capital": "Maputo"},
{"country":"Namibia", "capital": "Windhoek"},
{"country":"South Africa", "capital": "Pretoria"},
{"country":"Swaziland", "capital": "Lobamba"},
{"country":"Tanzania", "capital": "Dodoma"},
{"country":"Zambia", "capital": "Lusaka"},
{"country":"Zimbabwe", "capital": "Harare"}
]
}
这是错误日志:http: //pastebin.com/K5Qjh6h4
谢谢你。