Java 获取json在本地主机和一些网站上有效,但在我的真实服务器上不起作用

Java 获取json在本地主机和一些网站上有效,但在我的真实服务器上不起作用,java,php,android,json,server,Java,Php,Android,Json,Server,我有一个从我的服务器(在线)获取信息的应用程序,我的计算机上有一个该网站的版本(localhost) 应用程序正在localhost上运行,但我无法访问服务器上的网站。 新的 HttpAsyncTask()。执行(“”) HttpAsyncTask类: private class HttpAsyncTask extends AsyncTask<String, Void, String> { @Override protected String doInBackgrou

我有一个从我的服务器(在线)获取信息的应用程序,我的计算机上有一个该网站的版本(localhost)

应用程序正在localhost上运行,但我无法访问服务器上的网站。

新的 HttpAsyncTask()。执行(“”)

HttpAsyncTask类:

private class HttpAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {


        return GET(urls[0]);
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();




        try {
            JSONObject json = new JSONObject(result);
            etResponse.setText(json.toString(1));

        } catch (Exception e) {
            // TODO: handle exception
        }
   }
}

  public static String GET(String url){
        InputStream inputStream = null;
        String result = "";
        try {

            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            // make GET request to the given URL
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // convert inputstream to string
            if(inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";

        } catch (Exception e) 
        {

        }

        return result;
    }

JSON是URL编码的。试试这个:

try{
    JSONObject json = new JSONObject(URLDecoder.decode(result, "UTF-8"));
    etResponse.setText(json.toString(1));
} catch (Exception e) {
    // TODO: handle exception
}

我不知道您在哪里调用此AsyncTask,但我看到的唯一其他可能返回运行时的错误是未初始化的静态方法。尝试将代码移动到AsyncTask中。

此代码不起作用。但是这在本地hostnvm中有效,问题在于get方法,我马上编辑答案。
try{
    JSONObject json = new JSONObject(URLDecoder.decode(result, "UTF-8"));
    etResponse.setText(json.toString(1));
} catch (Exception e) {
    // TODO: handle exception
}