Java 从服务器接收测试JSON字符串并显示在屏幕上
我已经从服务器收到了测验JSON字符串。这个字符串包含10个问题,我收到并将响应存储在Java 从服务器接收测试JSON字符串并显示在屏幕上,java,android,json,arraylist,Java,Android,Json,Arraylist,我已经从服务器收到了测验JSON字符串。这个字符串包含10个问题,我收到并将响应存储在ArrayList中。现在我想一个问题(和它的4个选项)应该显示在屏幕上,然后当我按下下一个按钮时,下一个问题应该同样显示在屏幕上,直到控制达到所有10个问题。我的问题是,我无法执行“按下一步”按钮显示第二个(下一个)问题的功能。请任何人帮助我,我是android初学者,谢谢 这里是JSON字符串 { "status": 200, "status_message": "Success", "
ArrayList
中。现在我想一个问题(和它的4个选项)应该显示在屏幕上,然后当我按下下一个按钮时,下一个问题应该同样显示在屏幕上,直到控制达到所有10个问题。我的问题是,我无法执行“按下一步”按钮显示第二个(下一个)问题的功能。请任何人帮助我,我是android初学者,谢谢
这里是JSON字符串
{
"status": 200,
"status_message": "Success",
"response":
[
{
"quizNumber" : "1",
"image" : "",
"question" : "Which car manufacturer was the first to win 100 F1 races?",
"option1" : "Ferrari",
"option2" : "Nissan",
"option3" : "Ford",
"option4" : "",
"answer" : "Ferrari."
},
{
"quizNumber" : "2",
"image" : "",
"question" : "In the professional era which woman has won the most titles at Wimbledon [singles, doubles and mixed] ?",
"option1" : "Venus",
"option2" : "Hingis",
"option3" : "Martina Navratilova",
"option4" : "Waynetta",
"answer" : "Martina Navratilova"
},
{
"quizNumber" : "3",
"image" : "",
"question" : "How many times have Liverpool been relegated from the top flight of English football?",
"option1" : "Four",
"option2" : "Three",
"option3" : "Two",
"option4" : "Five",
"answer" : "Three"
}]}
这是我的MainActivity.java类
package com.example.quistest;
//import goes here
public class MainActivity extends Activity {
String serviceUrl;
ImageView next;
TextView question,quizno;
CheckBox ans_1,ans_2,ans_3,ans_4;
ArrayList<Quiz_List>data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=new ArrayList<Quiz_List>();
next=(ImageView) findViewById(R.id.imageView_nextID);
quizno=(TextView) findViewById(R.id.question_noID);
question=(TextView) findViewById(R.id.txt_questionID);
ans_1=(CheckBox) findViewById(R.id.chk_ans1ID);
ans_2=(CheckBox) findViewById(R.id.chk_ans2ID);
ans_3=(CheckBox) findViewById(R.id.chk_ans3ID);
ans_4=(CheckBox) findViewById(R.id.chk_ans4ID);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Next...", Toast.LENGTH_SHORT).show();
}
});
if (isNetworkAvailable()) {
execute();
}
else {
// Error message here if network is unavailable.
Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show();
}
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
private void execute() {
serviceUrl ="http://mobile.betfan.com/api/?action=quiz&key=MEu07MgiuWgXwJOo7Oe1aHL0yM8VvP&sporttype=all";
class LoginAsync extends AsyncTask<String, Void, String>{
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(MainActivity.this, "Please while wait", "Loading...");
}
@Override
protected String doInBackground(String... params) {
JSONObject jsonObject = new JSONObject();
String dataString = jsonObject.toString();
InputStream is = null;
List<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data", dataString));
String result = null;
try{
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpRequest = new HttpGet();
URI apiurl = new URI(serviceUrl);
httpRequest.setURI(apiurl);
HttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
protected void onPostExecute(String result){
String s = result.trim();
loadingDialog.dismiss();
JSONObject respObject;
try {
respObject = new JSONObject(s);
String active = respObject.getString("status_message");
if(active.equalsIgnoreCase("success")){
JSONArray array = respObject.getJSONArray("response");
for (int i =0; i<array.length();i++){
JSONObject jsonObject = array.getJSONObject(i);
String quizNumber= jsonObject.getString("quizNumber");
String question= jsonObject.getString("question");
String option1 = jsonObject.getString("option1");
String option2 = jsonObject.getString("option2");
String option3 = jsonObject.getString("option3");
String option4 = jsonObject.getString("option4");
data.add(new Quiz_List(quizNumber,question,option1,option2,option3,option4));
quizno.setText("Question numer:"+quizNumber);
MainActivity.this.question.setText(question);
ans_1.setText(option1);
ans_2.setText(option2);
ans_3.setText(option3);
ans_4.setText(option4);
}
}else {
Toast.makeText(MainActivity.this, "Quiz received Fail", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
LoginAsync la = new LoginAsync();
la.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package com.example.quistest;
public class Quiz_List {
private String quiz_no;
private String question;
private String answer_1;
private String answer_2;
private String answer_3;
private String answer_4;
public String getQuiz_no() {
return quiz_no;
}
public void setQuiz_no(String quiz_no) {
this.quiz_no = quiz_no;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getAnswer_1() {
return answer_1;
}
public void setAnswer_1(String answer_1) {
this.answer_1 = answer_1;
}
public String getAnswer_2() {
return answer_2;
}
public void setAnswer_2(String answer_2) {
this.answer_2 = answer_2;
}
public String getAnswer_3() {
return answer_3;
}
public void setAnswer_3(String answer_3) {
this.answer_3 = answer_3;
}
public String getAnswer_4() {
return answer_4;
}
public void setAnswer_4(String answer_4) {
this.answer_4 = answer_4;
}
public Quiz_List(String quiz_no, String question, String answer_1,
String answer_2, String answer_3, String answer_4) {
super();
this.quiz_no = quiz_no;
this.question = question;
this.answer_1 = answer_1;
this.answer_2 = answer_2;
this.answer_3 = answer_3;
this.answer_4 = answer_4;
}
}
请任何人帮助我想当我想按下一步,然后它应该显示第二个问题(其4个选项)应该显示在屏幕上。
在当前情况下,它只显示最后一个问题(其4个选项) 当需要将数据从一个活动传递到另一个活动时,需要使用
intent.putExtra()
方法发送数据
从您的主要活动发送数据->
Intent intent = new Intent(MainActivity.this,NEXT_ACTIVITY.class); // Change NEXT_ACTIVITY to your option / answer activity.
intent.putExtra("option_1",option1);
intent.putExtra("option_2",option2);
intent.putExtra("option_3",option3);
intent.putExtra("option_4",option4);
intent.putExtra("answer",answer);
startActivity(intent);
Bundle bundle = getIntent().getExtras();
String option1 = bundle.getString("option_1");
String option2 = bundle.getString("option_2");
String option3 = bundle.getString("option_3");
String option4 = bundle.getString("option_4");
String answer = bundle.getString("answer");
要从下一个活动接收数据->
Intent intent = new Intent(MainActivity.this,NEXT_ACTIVITY.class); // Change NEXT_ACTIVITY to your option / answer activity.
intent.putExtra("option_1",option1);
intent.putExtra("option_2",option2);
intent.putExtra("option_3",option3);
intent.putExtra("option_4",option4);
intent.putExtra("answer",answer);
startActivity(intent);
Bundle bundle = getIntent().getExtras();
String option1 = bundle.getString("option_1");
String option2 = bundle.getString("option_2");
String option3 = bundle.getString("option_3");
String option4 = bundle.getString("option_4");
String answer = bundle.getString("answer");
现在,您可以在本活动中的任何位置使用这些数据 若您解析json并在数组列表中添加数据,我建议您创建一个pojo类并获取pojo类型数组列表中的所有值。当您按下“下一步”按钮时,查找数组列表大小和数组列表索引从0开始到数组列表大小,因此取一个整数
(int i=0)
,其初始值必须为0(零)。现在使用if(i我根据您的要求编写一个示例
1-我们有一个json数组
{
“地位”:200,
“状态信息”:“成功”,
“答复”:
[
{
“quizNumber”:“1”,
“图像”:“,
“问题”:“哪家汽车制造商是第一个赢得100场F1比赛的?”,
“选项1”:“法拉利”,
“选项2”:“日产”,
“选项3”:“福特”,
“选择权4”:“,
回答:“法拉利。”
},
{
“quizNumber”:“2”,
“图像”:“,
“问题”:“在职业时代,哪位女性在温布尔登(单打、双打和混合赛)获得最多冠军?”,
“选项1”:“维纳斯”,
“选项2”:“辛吉斯”,
“选项3”:“Martina Navratilova”,
“选项4”:“Waynetta”,
回答:“玛蒂娜·纳夫拉蒂洛娃”
},
{
“quizNumber”:“3”,
“图像”:“,
“问题”:“利物浦多少次从英格兰足球顶级联赛降级?”,
“选项1”:“四个”,
“选项2”:“三个”,
“选项3”:“两个”,
“选项4”:“五个”,
答:"三"
}]}
2-做一个pojo类
Mypojo类{
字符串op1、op2、op3、op4、ans;
Mypojo(字符串s1、字符串s2、字符串s3、字符串s4、字符串ss){
op1=s1;
.......
ans=ss;
}
//在此处添加getter/setter方法
}
3-现在在活动中获取数组列表
int i=0;
ArrayList mypojo=new AarrayList();
解析json的地方
使pojo类对象类似
Mypojo pojo=新建Mypojo(参数)并传递所有参数
然后是mypojo.add(pojo);
这样,所有json数据都将添加到pojo类型数组列表中。
4-现在在下一个按钮代码将是
int arraysize=mypojo.size();
如果(i初始设置在主活动中
int count=0;
package com.example.quistest;
//import goes here
public class MainActivity extends Activity {
String serviceUrl;
ImageView next;
TextView question,quizno;
CheckBox ans_1,ans_2,ans_3,ans_4;
ArrayList<Quiz_List>data;
String count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=new ArrayList<Quiz_List>();
next=(ImageView) findViewById(R.id.imageView_nextID);
quizno=(TextView) findViewById(R.id.question_noID);
question=(TextView) findViewById(R.id.txt_questionID);
ans_1=(CheckBox) findViewById(R.id.chk_ans1ID);
ans_2=(CheckBox) findViewById(R.id.chk_ans2ID);
ans_3=(CheckBox) findViewById(R.id.chk_ans3ID);
ans_4=(CheckBox) findViewById(R.id.chk_ans4ID);
Intent i=getIntent();
count=i.getStringExtra("count");
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int new_count=Integer.parseInt(count)+1;
Intent intent=new Intent(MainActivity.this, MainActivity.class);
intent.putExtra("count",String.valueOf(new_count));
Toast.makeText(getApplicationContext(), "Next...", Toast.LENGTH_SHORT).show();
}
});
if (isNetworkAvailable()) {
execute();
}
else {
// Error message here if network is unavailable.
Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show();
}
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
private void execute() {
serviceUrl ="http://mobile.betfan.com/api/?action=quiz&key=MEu07MgiuWgXwJOo7Oe1aHL0yM8VvP&sporttype=all";
class LoginAsync extends AsyncTask<String, Void, String>{
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(MainActivity.this, "Please while wait", "Loading...");
}
@Override
protected String doInBackground(String... params) {
JSONObject jsonObject = new JSONObject();
String dataString = jsonObject.toString();
InputStream is = null;
List<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data", dataString));
String result = null;
try{
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpRequest = new HttpGet();
URI apiurl = new URI(serviceUrl);
httpRequest.setURI(apiurl);
HttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
protected void onPostExecute(String result){
String s = result.trim();
loadingDialog.dismiss();
JSONObject respObject;
try {
respObject = new JSONObject(s);
String active = respObject.getString("status_message");
if(active.equalsIgnoreCase("success")){
JSONArray array = respObject.getJSONArray("response");
JSONObject jsonObject = array.getJSONObject(Integer.parseInt(count));
String quizNumber= jsonObject.getString("quizNumber");
String question= jsonObject.getString("question");
String option1 = jsonObject.getString("option1");
String option2 = jsonObject.getString("option2");
String option3 = jsonObject.getString("option3");
String option4 = jsonObject.getString("option4");
data.add(new Quiz_List(quizNumber,question,option1,option2,option3,option4));
quizno.setText("Question numer:"+quizNumber);
MainActivity.this.question.setText(question);
ans_1.setText(option1);
ans_2.setText(option2);
ans_3.setText(option3);
ans_4.setText(option4);
}else {
Toast.makeText(MainActivity.this, "Quiz received Fail", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
LoginAsync la = new LoginAsync();
la.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
单击“下一步”按钮时,将1添加到计数并将计数发送到MainActivity.class
Intent i=new Intent(MainActivity.this,MainActivity.class);
i.putStringExtra("count",count);
startActivity(i);
并根据post Execute中的接收计数
JSONObject jsonObject = array.getJSONObject(count);
String quizNumber= jsonObject.getString("quizNumber");
String question= jsonObject.getString("question");
String option1 = jsonObject.getString("option1");
String option2 = jsonObject.getString("option2");
String option3 = jsonObject.getString("option3");
String option4 = jsonObject.getString("option4");
每次单击“下一步”按钮,计数将增加一
将下面的内容粘贴到MainActivity中
int count=0;
package com.example.quistest;
//import goes here
public class MainActivity extends Activity {
String serviceUrl;
ImageView next;
TextView question,quizno;
CheckBox ans_1,ans_2,ans_3,ans_4;
ArrayList<Quiz_List>data;
String count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=new ArrayList<Quiz_List>();
next=(ImageView) findViewById(R.id.imageView_nextID);
quizno=(TextView) findViewById(R.id.question_noID);
question=(TextView) findViewById(R.id.txt_questionID);
ans_1=(CheckBox) findViewById(R.id.chk_ans1ID);
ans_2=(CheckBox) findViewById(R.id.chk_ans2ID);
ans_3=(CheckBox) findViewById(R.id.chk_ans3ID);
ans_4=(CheckBox) findViewById(R.id.chk_ans4ID);
Intent i=getIntent();
count=i.getStringExtra("count");
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int new_count=Integer.parseInt(count)+1;
Intent intent=new Intent(MainActivity.this, MainActivity.class);
intent.putExtra("count",String.valueOf(new_count));
Toast.makeText(getApplicationContext(), "Next...", Toast.LENGTH_SHORT).show();
}
});
if (isNetworkAvailable()) {
execute();
}
else {
// Error message here if network is unavailable.
Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show();
}
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
private void execute() {
serviceUrl ="http://mobile.betfan.com/api/?action=quiz&key=MEu07MgiuWgXwJOo7Oe1aHL0yM8VvP&sporttype=all";
class LoginAsync extends AsyncTask<String, Void, String>{
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(MainActivity.this, "Please while wait", "Loading...");
}
@Override
protected String doInBackground(String... params) {
JSONObject jsonObject = new JSONObject();
String dataString = jsonObject.toString();
InputStream is = null;
List<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data", dataString));
String result = null;
try{
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpRequest = new HttpGet();
URI apiurl = new URI(serviceUrl);
httpRequest.setURI(apiurl);
HttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
protected void onPostExecute(String result){
String s = result.trim();
loadingDialog.dismiss();
JSONObject respObject;
try {
respObject = new JSONObject(s);
String active = respObject.getString("status_message");
if(active.equalsIgnoreCase("success")){
JSONArray array = respObject.getJSONArray("response");
JSONObject jsonObject = array.getJSONObject(Integer.parseInt(count));
String quizNumber= jsonObject.getString("quizNumber");
String question= jsonObject.getString("question");
String option1 = jsonObject.getString("option1");
String option2 = jsonObject.getString("option2");
String option3 = jsonObject.getString("option3");
String option4 = jsonObject.getString("option4");
data.add(new Quiz_List(quizNumber,question,option1,option2,option3,option4));
quizno.setText("Question numer:"+quizNumber);
MainActivity.this.question.setText(question);
ans_1.setText(option1);
ans_2.setText(option2);
ans_3.setText(option3);
ans_4.setText(option4);
}else {
Toast.makeText(MainActivity.this, "Quiz received Fail", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
LoginAsync la = new LoginAsync();
la.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package com.example.quistest;
//进口到这里
公共类MainActivity扩展了活动{
字符串serviceUrl;
图像查看下一步;
文本视图问题;
复选框ANSU 1、ANSU 2、ANSU 3、ANSU 4;
ArrayListdata;
字符串计数=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
数据=新的ArrayList();
next=(ImageView)findViewById(R.id.ImageView\u nextID);
quizno=(TextView)findViewById(R.id.question\u noID);
问题=(TextView)findViewById(R.id.txt\u questionID);
ans_1=(复选框)findViewById(R.id.chk_ans1ID);
ans_2=(复选框)findViewById(R.id.chk_ans2ID);
ans_3=(复选框)findViewById(R.id.chk_ans3ID);
ans_4=(复选框)findViewById(R.id.chk_ans4ID);
Intent i=getIntent();
计数=i.getStringExtra(“计数”);
next.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
int new_count=Integer.parseInt(count)+1;
意向意向=新意向(MainActivity.this,MainActivity.class);
intent.putExtra(“count”,String.valueOf(new_count));
Toast.makeText(getApplicationContext(),“下一步…”,Toast.LENGTH\u SHORT.show();
}
});
if(isNetworkAvailable()){
执行();
}
否则{
//如果网络不可用,此处显示错误消息。
Toast.makeText(此“网络不可用!”,Toast.LENGTH_LONG.show();
}
}
专用布尔值isNetworkAvailable(){
ConnectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo NetworkInfo=manager.getActiveNetworkInfo();
布尔值isAvailable=false;
if(networkInfo!=null&&networkInfo.isConnected()){
isAvailable=真;
}
可获得的回报;
}
私有void execute(){
服务URL=”http://mobile.betfan.com/api/?action=quiz&key=MEu07MgiuWgXwJOo7Oe1aHL0yM8VvP&sporttype=all";
类LoginAsync扩展了AsyncTask{
私有对话框加载对话框;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
loadingDialog=ProgressDialog.show(MainActivity.this,“请稍候”,“正在加载…”);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
JSONObject JSONObject=新的JSONObject();
字符串dataString=jsonObject.toString();
InputStream=nul