我想在JSON
的{{1}}中显示此TextView
。
这是我的Activity
,
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
中显示JSON
,
TextView
我应该把TextView的代码放在哪里?
请帮帮我们。提前谢谢。
答案 0 :(得分:1)
一种方法是将您的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);
}
答案 1 :(得分:0)
将AsyncTask更改为以下内容:
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);
}
}
答案 2 :(得分:0)
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);
}
}
}
答案 3 :(得分:0)
1.add tv = (TextView)findViewById(R.id.txt);
//在<{p>}之前的onCreate
方法中添加此行
new GetContacts().execute(); line.
更改
private class GetContacts extends AsyncTask<Void,Void,Void>
与
private class GetContacts extends AsyncTask<String,Void,Void>
因为您需要String在结果中显示在TextView
return name;
而不是doInBackground方法中的null
protected void onPostExecute(Void result){
用String替换参数中的Void,因为我们将得到String中的结果。tv.setText(result);
方法中super.onPostExecute(result);
之后添加onPostExecute
。答案 4 :(得分:0)
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);
}
}
}