【问题标题】:I can't retrieve data from server to android studio although data is shown in a browser尽管数据显示在浏览器中,但我无法将数据从服务器检索到 android studio
【发布时间】: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


【解决方案1】:

你可以在新的volley库中使用StringRequest而不是jsonObject

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

【讨论】:

  • 感谢您的回复,使用字符串请求比使用 jsonObject 更容易
【解决方案2】:

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

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();
    }

【讨论】:

【解决方案3】:

问题是您的服务器的响应中没有设置Content-Type 标头。为了让 Volley 能够解析它,您需要将该标头设置为 application/json

在您的show.php 中添加以下行:

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

编辑

经过上面的改动,反应好像很奇怪……

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

【讨论】:

  • 我在 show.php 中添加了这一行,但应用程序中没有显示数据。这是php文件drive.google.com/file/d/0B_9G3EhgCUpDbjhLdmxOZEtDU2c/…的截图
  • 我也可以看到它在文件顶部设置为文本/纯文本。去掉上面的那个,把这个放在上面。
  • 虽然这不会改变什么,但最后一个是有效的。
猜你喜欢
  • 1970-01-01
  • 2020-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
  • 1970-01-01
  • 2021-10-22
相关资源
最近更新 更多