我是新來的android。 這裏我試圖讓一個應用程序從Json網站獲取數據,並在我試圖自動刷新活動時顯示它,它顯示新老數據都可以幫助我。在文本視圖中自動刷新
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultview1=(TextView) findViewById(R.id.team1_1);
resultview2=(TextView) findViewById(R.id.team1_2);
resultview3=(TextView) findViewById(R.id.date);
resultview4=(TextView) findViewById(R.id.comm1);
resultview5=(TextView) findViewById(R.id.team2_1);
resultview6=(TextView) findViewById(R.id.team2_2);
resultview7=(TextView) findViewById(R.id.comm2);
resultview8=(TextView) findViewById(R.id.team3_1);
resultview9=(TextView) findViewById(R.id.team3_2);
resultview10=(TextView) findViewById(R.id.comm3);
resultview11=(TextView) findViewById(R.id.team4_1);
resultview12=(TextView) findViewById(R.id.team4_2);
resultview13=(TextView) findViewById(R.id.comm4);
new BackHelper().execute();
//mHandler();
}
/*private void mHandler()
{
// TODO Auto-generated method stub
mHandler.postDelayed(updateTask, 15000);
} */
public void getdata1()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s1= s1 +
"Name : "+json.getString("t1")+"\n"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"\n"+
"Over : "+json.getInt("t1_0");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata2()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s2= s2 +
"Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
"Over : "+json.getInt("t2_o");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata3()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s3= s3
+json.getString("date");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata4()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s4= s4
+json.getString("comm");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata5()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team2.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s5= s5 +
"Name : "+json.getString("t1")+"\n"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"\n"+
"Over : "+json.getInt("t1_0");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata6()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team2.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s6= s6 +
"Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
"Over : "+json.getInt("t2_o");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata7()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team2.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s7= s7+json.getString("comm");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata8()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team3.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s8= s8 +
"Name : "+json.getString("t1")+"\n"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"\n"+
"Over : "+json.getInt("t1_0");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata9()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team3.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s9= s9 +
"Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
"Over : "+json.getInt("t2_o");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata10()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team3.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s10= s10 +json.getString("comm");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata11()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team4.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s11= s11 +
"Name : "+json.getString("t1")+"\n"+"Run : "+json.getInt("t1_r")+"/"+json.getInt("t1_w")+"\n"+
"Over : "+json.getInt("t1_0");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata12()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team4.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s12= s12 +
"Name : "+json.getString("t2")+"\n"+"Run : "+json.getInt("t2_r")+"/"+json.getInt("t2_w")+"\n"+
"Over : "+json.getInt("t2_o");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
public void getdata13()
{
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.3gsngarbagroup.com/master/team4.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
isr= entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
//resultview.setText("could not connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag","Error converting result"+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++)
{
JSONObject json = jArray.getJSONObject(i);
s13= s13 +json.getString("comm");
}
//resultview.setText(s);
}catch (Exception e){
Log.e("log_tag","Error Parsing Data"+e.toString());
}
}
/* private Handler mHandler = new Handler();
private Runnable updateTask = new Runnable()
{
public void run()
{
Log.d(getString(R.string.app_name) + " ChatList.updateTask()",
"updateTask run!");
// run any code here...
//getdata();
new BackHelper().execute();
Log.e("hell", "hello");
// queue the task to run again in 15 seconds...
mHandler.postDelayed(updateTask, 15000);
}
};*/
/*private final static int Interval=1000;
Handler mHandler;
Runnable mHandlerTask=new Runnable() {
public void run() {
// TODO Auto-generated method stub
new BackHelper().execute();
mHandler.postDelayed(mHandlerTask, Interval);
}
};
void startrepeatingTask()
{
mHandlerTask.run();
}
void stopRepeatingTask()
{
mHandler.removeCallbacks(mHandlerTask);
}*/
class BackHelper extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params)
{
getdata1();
getdata2();
getdata3();
getdata4();
getdata5();
getdata6();
getdata7();
getdata8();
getdata9();
getdata10();
getdata11();
getdata12();
getdata13();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//super.onPostExecute(result1);
resultview1.setText(s1);
resultview2.setText(s2);
resultview3.setText(s3);
resultview4.setText(s4);
resultview5.setText(s5);
resultview6.setText(s6);
resultview7.setText(s7);
resultview8.setText(s8);
resultview9.setText(s9);
resultview10.setText(s10);
resultview11.setText(s11);
resultview12.setText(s12);
resultview13.setText(s13);
}
}
}
爲什麼你'Concat'舊字符串與新? 's2 = s2 + ....',只需設置新的字符串到你的''字符串 –
可能dublicate的http://stackoverflow.com/questions/21073664/auto-refresh-textview幾乎完全相同的代碼。 –
Vishal只是使用:s(n)= jsonValue; –