我最近在 android studio 2.3 中制作了一个应用程序来显示来自服务器的数据我使用的链接在浏览器中正常工作并且数据以 json 格式成功显示。但是在我的应用程序中我无法检索这些数据,我在我的应用程序 gradle 文件中添加了 avolley 库,如下所示:
compile 'com.mcxiaoke.volley:library:1.0.18'
我在 MainActivity 中使用的代码是:
public class MainActivity extends AppCompatActivity {
RequestQueue rq;
String url = "http://abdulwahid.esy.es/show.php";
TextView txtshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtshow = (TextView) findViewById(R.id.txtshow);
rq = Volley.newRequestQueue(this);
JsonObjectRequest jor = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jarr = response.getJSONArray("allstudents");
for (int i = 0; i < jarr.length(); i++) {
JSONObject res = jarr.getJSONObject(i);
String id = res.getString("id");
String name = res.getString("name");
String info = res.getString("info");
txtshow.append("\n" + id + " - " + name + "\n" + info + "\n" + "------------------" + "\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", "ERROR");
}
});
rq.add(jor);
}
我使用的 php 文件链接在网络浏览器中成功执行。 如何在我的应用程序中显示数据,或者是否有其他代码或库可用于在线检索数据表单?
最佳答案
在新的 volley 库中,你可以使用 StringRequest 而不是 jsonObject
compile 'com.android.volley:volley:1.0.0'
关于android - 尽管数据显示在浏览器中,但我无法将数据从服务器检索到 android studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42885142/