gpt4 book ai didi

android - 如何在 Android 的 TextView 中显示 JSON 文件?

转载 作者:行者123 更新时间:2023-11-30 00:58:50 26 4
gpt4 key购买 nike

我想在我的 Activity 中的 TextView 中显示此 JSON

这是我的JSON

{
"AboutUs": [{
"p_content": "Dr. Vineet Malhotra currently working as a Consultant urologist where job description requires to run the urology units at these hospitals and perform various endourology, reconstructive and laparoscopic procedures. Dr. Vineet Malhotra associated with the department of urology for the last ten years and have been exposed to different practice methodologies.Dr. Vineet Malhotra have a special interest in recent advances in minimally invasive procedures in urology.\n"
}]
}

这是我的 Activity,我想在 TextView 中显示此 JSON

public class AboutUs extends AppCompatActivity {
private String TAG=MainActivity.class.getSimpleName();
private ProgressDialog pDialog;

TextView tv;
private static String url="http://softappsdemo.com/doctorplus/";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_us);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

new GetContacts().execute();
}


private class GetContacts extends AsyncTask<Void,Void,Void> {
protected void onPreExecute(){
super.onPreExecute();
pDialog=new ProgressDialog(AboutUs.this);
pDialog.setMessage("Please Wait ......");
pDialog.setCancelable(false);
pDialog.show();
}
protected Void doInBackground(Void... arg){
Handler sh=new Handler();
String jsonStr=sh.makeServiceCall(url);
Log.e(TAG,"Response from url : " +jsonStr);
if(jsonStr!=null){

try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray contacts = jsonObj.getJSONArray("AboutUs");

JSONObject c = contacts.getJSONObject(0);

String name = c.getString("p_content");




}catch (final JSONException e){
Log.e(TAG,"Json parsing error: " +e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"JSON parsing error: " +e.getMessage(),Toast.LENGTH_LONG).show();
}
});
}

}else {
Log.e(TAG,"Could not get JSON from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"could not get JSOM from server check logcat if possible " ,Toast.LENGTH_LONG).show();
}
});
}

return null;
}
protected void onPostExecute(Void result){
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
}
}
}

我应该在哪里放置 TextView 的代码?

请大家帮帮我。提前致谢。

最佳答案

一种方法是将您的 String 名称变量作为全局变量,并在 onPostExecute 方法中设置 TextView 中的内容。

public class AboutUs extends AppCompatActivity {
private String name;

.............................
..........................


protected void onPostExecute(String result){
super.onPostExecute(result);
if (result != null)
myTextView.setText(name);

}

关于android - 如何在 Android 的 TextView 中显示 JSON 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39747841/

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