无法从服务器获取数据,即使我textview
根据onCreate()
中的条件有价值,当我删除此条件时works
就好了对我来说(我的意思是,在那种情况下,我是来自服务器的able to fetch JSON data
。
我试了很多东西,但仍然没有成功,总是得到"Unable to fetch data from server"
,因为我在AsyncTask的onPostExecute()
方法中使用它作为Toast
我正在为 TextView 获取value
,这肯定也是 AsyncTask 的调用execute
方法,但始终无法获取来自服务器的数据
你有什么问题:
1)为什么我在onCreate()
中使用此类条件,然后检查我的question
2)为什么我在onPostExecute()
设置适配器,然后检查我的question
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_category);
etTextToSave = (TextView) findViewById(R.id.textView2);
sharedprefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
etTextToSave.setText(sharedprefs.getString("SharedPrefsData",""));
actorsList = new ArrayList<VideoCategory>();
if(etTextToSave.getText().toString().length()>0)
{
new JSONAsyncTask().execute("http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors");
}
centerLockHorizontalScrollview = (HSVActivity) findViewById(R.id.scrollView);
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@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);
JSONArray jarray = jsono.getJSONArray("actors");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Actors actor = new Actors();
actor.setName(object.getString("name"));
Log.d("Name:", object.getString("name"));
actor.setImage(object.getString("image"));
Log.d("Image:", object.getString("image"));
actorsList.add(actor);
}
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
if(result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
if(actorsList != null) {
adapter = new VideoCategoryAdapter(getApplicationContext(), R.layout.adapter_video_category, actorsList);
centerLockHorizontalScrollview.setAdapter(HomeActivity.this, adapter);
}
}
}
答案 0 :(得分:1)
我没有收到任何异常或Toast消息。 检查Json响应。
答案 1 :(得分:1)
根据您发布的问题,您可以从服务器获取数据 Toast “无法从服务器获取数据”。我在代码中做了一些小改动,请尝试一下,让我知道。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_category);
etTextToSave = (TextView) findViewById(R.id.textView2);
sharedprefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
etTextToSave.setText(sharedprefs.getString("SharedPrefsData",""));
actorsList = new ArrayList<VideoCategory>();
if(etTextToSave.getText().toString().length()>0)
{
new JSONAsyncTask().execute("http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors");
}
centerLockHorizontalScrollview = (HSVActivity) findViewById(R.id.scrollView);
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
JSONObject jObj;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@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);
jObj = new JSONObject(data);
if(!(jObj.equals(null)))
{
JSONArray jarray = jsono.getJSONArray("actors");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Actors actor = new Actors();
actor.setName(object.getString("name"));
Log.d("Name:", object.getString("name"));
actor.setImage(object.getString("image"));
Log.d("Image:", object.getString("image"));
actorsList.add(actor);
}
}
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
if(jObj== null)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
if(actorsList != null) {
adapter = new VideoCategoryAdapter(getApplicationContext(), R.layout.adapter_video_category, actorsList);
centerLockHorizontalScrollview.setAdapter(HomeActivity.this, adapter);
}
}
}