【发布时间】:2017-03-19 09:51:06
【问题描述】:
我最近在 android studio 2.3 中创建了一个应用程序来显示来自服务器的数据,我使用的链接在浏览器中正常工作,并且数据以 json 格式成功显示。但是在我的应用程序中我无法检索这些数据,我在我的应用程序 gradle 文件中添加了 avolley lib,如下所示:
编译 '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 文件的链接在网络浏览器中成功执行。 如何在我的应用程序中显示数据,或者是否有其他代码或库可用于在线检索数据表单?
【问题讨论】:
-
出了什么问题?
-
The link of php file that I use is executed successfully。你怎么知道的? -
@greenapps,我在网络浏览器中试了一下,它成功了。
-
出了什么问题????
-
@greenapps 错误是当我在 android studio 中使用上面的代码并在设备或模拟器中运行它时,应用程序中没有显示任何内容。
标签: android android-studio android-volley