【发布时间】:2016-03-14 07:06:28
【问题描述】:
我正在做一个可以添加的 AsyncTask 如果我将我的 sdk 降低到 22。
在 sdk 23 上它根本不起作用,即使在 22 中,它也为我写了一些行。
我做错了什么? 或者如何更正它以便我可以在 sdk 23 上运行它?
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
//this gets us the actors node and puts it in jarray veriable
JSONArray jarray = jsono.getJSONArray("actors");
for (int i = 0; i < jarray.length(); i++) {
//the object veriable will be the node in position i inside the "actors" node in the main json
JSONObject object = jarray.getJSONObject(i);
Log.d("myAppLog", "next one is: ");
Log.d("myAppLog", object.getString("name"));
actorClass actorClassVeriable = new actorClass();
actorClassVeriable.setName(object.getString("name"));
actorClassVeriable.setDescription(object.getString("description"));
actorClassVeriable.setDob(object.getString("dob"));
actorClassVeriable.setCountry(object.getString("country"));
actorClassVeriable.setHeight(object.getString("height"));
actorClassVeriable.setSpouse(object.getString("spouse"));
actorClassVeriable.setChildren(object.getString("children"));
actorClassVeriable.setImage(object.getString("image"));
actorsList.add(actorClassVeriable);
}
return true;
}
//------------------>>
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
if(result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
- 它记录了 > HttpGet、HttpClient、HttpResponse 和 HttpEntity
【问题讨论】:
-
它们已被弃用。只需进行搜索。 Example link