gpt4 book ai didi

java - 无法从服务器获取 JSON 数据

转载 作者:行者123 更新时间:2023-11-29 21:00:33 25 4
gpt4 key购买 nike

无法从服务器获取数据 即使根据我在 onCreate() 中的条件我在 textview 中有值,当我删除此条件时这工作对我来说很好(我的意思是,在那种情况下我能够从服务器获取JSON数据)。

我尝试了很多东西,但对我来说仍然没有成功,总是得到 “无法从服务器获取数据”,因为我在 onPostExecute() 方法中将其用作 Toast异步任务

我正在为 TextView 获取 value,这肯定也是调用 AsyncTaskexecute 方法,但总是无法从服务器获取数据

你有没有想过的问题:

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);
}
}
}

最佳答案

我没有收到任何异常或 toast 消息。检查 Json 响应。

enter image description here

关于java - 无法从服务器获取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26250054/

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