【问题标题】:JSON Response is coming on Genymotion (Emulator) but not on actual device (My android phone)JSON 响应出现在 Genymotion(模拟器)上,但不在实际设备上(我的 android 手机)
【发布时间】:2016-05-02 07:17:33
【问题描述】:

这是我正在检索 JSON 的异步任务,它在模拟器上运行良好,但是当我尝试在实际手机上检索它时它不起作用

public class BackgroundTask extends AsyncTask<Void,Void,String>
{
        String Json_url;
        String json_String;
        @Override

        protected void onPreExecute() {
           Json_url = "http://192.168.10.9/UANProject/uanjson.php";
        }

        @Override
        protected String doInBackground(Void... params) {

            try {
                URL url = new URL(Json_url);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = connection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();
                while((json_String = bufferedReader.readLine())!=null)
                {
                    stringBuilder.append(json_String+"\n");

                }
                bufferedReader.close();
                inputStream.close();
                connection.disconnect();

                return stringBuilder.toString().trim();
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return null;
            } catch (IOException e) {
                e.printStackTrace();
                return null;``
            }
 

这是在 gennymotion 上运行时显示的结果,但在实际设备上,结果未显示在 textview 中,未显示任何错误或异常

protected void onPostExecute(String result) {
            TextView tv = (TextView) findViewById(R.id.tvJsonResp);
            tv.setText(result);

            Json_String = result;
        }

【问题讨论】:

  • 你的真机可以连接到本地服务器吗?您的虚拟设备可能已连接到本地服务器,但真实设备未连接到它。检查并告诉我它是否有效
  • 检查你当前的IP地址并把192.168.10.9代替
  • 您的网址有问题。尝试您的代码时出现超时异常
  • 您的 Json_url 是在本地主机还是实际服务器上。请在您的设备中打开浏览器并复制粘贴Json_url。然后检查响应。
  • @Vivek 我在我的本地服务器上运行它。即XAMPP

标签: android json android-studio


【解决方案1】:

我认为您正在使用本地服务器。在服务器上托管代码并检查

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 2023-03-09
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    相关资源
    最近更新 更多