Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android的TextView中显示JSON文件?_Android_Json - Fatal编程技术网

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

如何在Android的TextView中显示JSON文件?,android,json,Android,Json,我想在我的活动中的文本视图中显示此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, recon

我想在我的
活动中的
文本视图中显示此
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"
    }]
}
这是我的
活动
,我想在
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();
        }
    }
}
public类AboutUs扩展了AppCompatActivity{
私有字符串标记=MainActivity.class.getSimpleName();
私人对话;
文本视图电视;
专用静态字符串url=”http://softappsdemo.com/doctorplus/";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u about\u us);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
新建GetContacts().execute();
}
私有类GetContacts扩展异步任务{
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新进度对话框(AboutUs.this);
pDialog.setMessage(“请稍候……”);
pDialog.setCancelable(假);
pDialog.show();
}
受保护的Void doInBackground(Void…arg){
Handler sh=新的Handler();
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
JSONArray contacts=jsonObj.getJSONArray(“AboutUs”);
JSONObject c=contacts.getJSONObject(0);
字符串名称=c.getString(“p_内容”);
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),“JSON解析错误:”+e.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}否则{
Log.e(标记“无法从服务器获取JSON”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),“如果可能,无法从服务器获取JSOM检查logcat”,Toast.LENGTH_LONG.show();
}
});
}
返回null;
}
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
if(pDialog.isShowing())
pDialog.disclose();
}
}
}
我应该把TextView的代码放在哪里


请帮帮我,伙计们。提前感谢。

将您的任务更改为以下内容:

private class GetContacts extends AsyncTask<Void,Void,String> {
    protected void onPreExecute(){
        super.onPreExecute();
        pDialog=new ProgressDialog(AboutUs.this);
        pDialog.setMessage("Please Wait ......");
        pDialog.setCancelable(false);
        pDialog.show();
    }
    protected String 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");

                    return name;


            }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(String result){
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        if (result != null)
            tv.setText(result);

    }
} 
私有类GetContacts扩展异步任务{
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新进度对话框(AboutUs.this);
pDialog.setMessage(“请稍候……”);
pDialog.setCancelable(假);
pDialog.show();
}
受保护的字符串doInBackground(Void…arg){
Handler sh=新的Handler();
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
JSONArray contacts=jsonObj.getJSONArray(“AboutUs”);
JSONObject c=contacts.getJSONObject(0);
字符串名称=c.getString(“p_内容”);
返回名称;
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),“JSON解析错误:”+e.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}否则{
Log.e(标记“无法从服务器获取JSON”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),“如果可能,无法从服务器获取JSOM检查logcat”,Toast.LENGTH_LONG.show();
}
});
}
返回null;
}
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
if(pDialog.isShowing())
pDialog.disclose();
如果(结果!=null)
tv.setText(结果);
}
} 

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

public class AboutUs extends AppCompatActivity {
    private String name;

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


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

    }
package.company.abhi.navugation;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.support.design.widget.NavigationView;
导入android.support.v4.view.GravityCompat;
导入android.support.v4.widget.DrawerLayout;
导入android.support.v7.app.ActionBarDrawerToggle;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.support.v7.widget.Toolbar;
导入android.util.Log;
导入android.view.MenuItem;
导入android.webkit.WebView;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
关于活动的公共类{
私有字符串标记=MainActivity.class.getSimpleName();
私人对话;
文本视图电视;
专用静态字符串url=”http://softappsdemo.com/doctorplus/";
@越过
package androidthirst.company.abhi.navugation;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

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,String> {
        protected void onPreExecute(){
            super.onPreExecute();
            pDialog=new ProgressDialog(AboutUs.this);
            pDialog.setMessage("Please Wait ......");
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected String 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");
                        return name;
                }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(String result){
            super.onPostExecute(result);
            if (pDialog.isShowing())
                pDialog.dismiss();
            tv.setText(result);

        }
    }
}
new GetContacts().execute(); line.
public class MainActivity extends AppCompatActivity {

TextView tv = null;
InputStream is = null;
public static final String URL = "http://www.projectconnect.com.br/transportefacil/api/pedido/list/";
String json;
HttpResponse httpResponse = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (TextView) findViewById(R.id.tv_json);

    Task task = new Task();
    task.execute();
}

private class Task extends AsyncTask<Bundle, Void, String> {

    protected String doInBackground(Bundle... parametros) {

        HttpGet httpGet = new HttpGet(URL);

        httpGet.setHeader("Content-type", "application/json");
        httpGet.setHeader("Accept-Encoding", "compress, gzip");
        httpGet.setHeader("Accept", "application/json");

        DefaultHttpClient httpClient = new DefaultHttpClient();

        try {
            httpResponse = httpClient.execute(httpGet);

            is = AndroidHttpClient.getUngzippedContent(httpResponse.getEntity());

            json = Util.converteGzipEmJson(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }

    protected void onPostExecute(String json) {
        tv.setText(json);
    }
}