我能';t从服务器检索数据到android studio,尽管数据显示在浏览器中

我能';t从服务器检索数据到android studio,尽管数据显示在浏览器中,android,android-studio,android-volley,Android,Android Studio,Android Volley,我最近在android studio 2.3中制作了一个应用程序,用于显示来自服务器的数据。我使用的链接在浏览器中正常工作,数据以json格式成功显示。但在我的应用程序中,我无法检索这些数据,我在我的应用程序gradle文件中添加了avolley lib,如下所示: 编译'com.mcxiaoke.volley:library:1.0.18' 我在MainActivity中使用的代码是: public class MainActivity extends AppCompatActivity {

我最近在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);
        }
public类MainActivity扩展了AppCompatActivity{
请求队列rq;
字符串url=”http://abdulwahid.esy.es/show.php";
文本视图txtshow;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtshow=(TextView)findViewById(R.id.txtshow);
rq=Volley.newRequestQueue(this);
JsonObjectRequest jor=新的JsonObjectRequest(Request.Method.GET,url,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray jarr=response.getJSONArray(“所有学生”);
for(int i=0;i
我使用的php文件链接在web浏览器中成功执行。
如何在我的应用程序中显示数据,或者是否有其他代码或库用于在线检索数据表单?

使用此代码检索数据,在字符串行中,您的数据将作为完整字符串追加,如浏览器中所示

public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String>{

            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new HttpPost("http://abdulwahid.esy.es/show.php");

                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");

                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result){
                myJSON=result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();
    }
public void getData(){
类GetDataJSON扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
DefaultHttpClient httpclient=新的DefaultHttpClient(新的BasicHttpParams());
HttpPost HttpPost=新的HttpPost(“http://abdulwahid.esy.es/show.php");
//取决于您的web服务
setHeader(“内容类型”、“应用程序/json”);
InputStream InputStream=null;
字符串结果=null;
试一试{
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
inputStream=entity.getContent();
//默认情况下,json是UTF-8
BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream,“UTF-8”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
结果=sb.toString();
}捕获(例外e){
//哎呀
}
最后{
尝试{if(inputStream!=null)inputStream.close();}捕获(异常挤压){}
}
返回结果;
}
@凌驾
受保护的void onPostExecute(字符串结果){
myJSON=结果;
showList();
}
}
GetDataJSON g=新的GetDataJSON();
g、 执行();
}

问题在于服务器的响应中没有设置
内容类型
标题。要让Volley能够解析它,您需要将该标题设置为
application/json

show.php
中添加以下行:

header('Content-Type: application/json');
编辑

在上述改变之后,反应似乎很奇怪


我可以在响应中看到奇怪的字符,这可能会导致客户端出现解析问题。

您可以在新的凌空库中使用StringRequest而不是jsonObject

编译'com.android.volley:volley:1.0.0'


好吧,出了什么问题?
我使用的php文件链接成功执行了
。你怎么知道?@greenapps,我在web浏览器中尝试了它,它成功运行了。好吧,出了什么问题????@greenapps错误的是,当我在android studio中使用上述代码并在设备或模拟器中运行时,应用程序中没有显示任何内容。我添加了这一点这是一个php文件的屏幕截图,我可以看到它也被设置为文本/纯文本,放在文件的顶部。删除顶部的一个,然后把这个放在顶部。虽然这不会改变任何事情,但最后一个是有效的。评论不是为了扩展讨论;这个对话已经结束。谢谢你的回复因此,使用字符串请求比使用jsonObject更容易