我正在使用volley连接到我的其余Web服务。我有一个.php地址,它返回一个json对象。我在我的Android应用程序中使用以下代码来获取该对象:
public void getData() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(Request.Method.GET,URI_AZMOON_QUESTIONS,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String s="t";
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
requestQueue.add(jsonArrayRequest);
}
但是此请求永远不会运行。我的意思是,当我调试程序时,它永远不会进入onResponse
和onErrorResponse
方法。
更新
这是我的代码:
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.shaz.zist.Adapter.AzmoonsAdapter;
import com.shaz.zist.Models.AzmoonQuestion;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import WebService.WebService;
public class GroupQuizActivity extends AppCompatActivity implements View.OnClickListener {
public static final String URI_AZMOON_QUESTIONS = "http://zahra-t.zili.ir/azmoon/read.php";
List<AzmoonQuestion> azmoons=new ArrayList<>();
AzmoonsAdapter adapter;
LinearLayout noNetwork;
ListView azmoonList;
ImageView refresh;
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_quiz);
noNetwork=findViewById(R.id.noconection);
azmoonList=findViewById(R.id.quizesList);
refresh=findViewById(R.id.refresh);
refresh.setOnClickListener(this);
requestQueue = Volley.newRequestQueue(this);
checkNetworkConnection();
}
private void checkNetworkConnection() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean netResult=false;
if(connectivityManager.getActiveNetworkInfo()!=null)
netResult=connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting();
if(!netResult){
noNetwork.setVisibility(View.VISIBLE);
}
else{
noNetwork.setVisibility(View.GONE);
setUpListView();
}
}
private void setUpListView() {
WebService wb=new WebService(this,azmoons);
adapter=new AzmoonsAdapter(this,azmoons);
azmoonList.setAdapter(adapter);
getData();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.refresh:{
checkNetworkConnection();
break;
}
}
}
public void getData() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
/*StringRequest rs=new StringRequest(URI_AZMOON_QUESTIONS, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String ss=response;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});*/
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, URI_AZMOON_QUESTIONS, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
requestQueue.add(jsonArrayRequest);
}
}
答案 0 :(得分:0)
尝试JsonArrayRequest的StringRequest接口(StringRequest始终对我有用)
StringRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, URI_AZMOON_QUESTIONS,
new Response.Listener<String>() {@Override
public void onResponse(JSONArray response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
requestQueue.add(jsonArrayRequest);
}