获取json可在localhost和某些网站上使用,但不能在我的真实服务器上使用 [英] Getting json works on localhost and some websites, but not working on my real server

查看:139
本文介绍了获取json可在localhost和某些网站上使用,但不能在我的真实服务器上使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an application that is getting information from my server (online), I have a version of that website in my computer (localhost),

正在本地主机上运行的应用程序可以获取json,但是我无法访问服务器上的网站.

新 HttpAsyncTask().execute(" http://alarbaeen .com/app/?a = gallery& b = images& c = new& d = 1 );

new HttpAsyncTask().execute("http://alarbaeen.com/app/?a=gallery&b=images&c=new&d=1");

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

这些是错误:

 03-05 12:24:32.735: E/AndroidRuntime(5859): FATAL EXCEPTION: AsyncTask #1
03-05 12:24:32.735: E/AndroidRuntime(5859): Process: com.example.testjsons22, PID: 5859
03-05 12:24:32.735: E/AndroidRuntime(5859): java.lang.RuntimeException: An error occured while executing doInBackground()
03-05 12:24:32.735: E/AndroidRuntime(5859):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at java.lang.Thread.run(Thread.java:841)
03-05 12:24:32.735: E/AndroidRuntime(5859): Caused by: java.lang.NullPointerException: println needs a message
03-05 12:24:32.735: E/AndroidRuntime(5859):     at android.util.Log.println_native(Native Method)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at android.util.Log.i(Log.java:160)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at com.example.testjsons22.MainActivity.GET(MainActivity.java:77)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at com.example.testjsons22.MainActivity$HttpAsyncTask.doInBackground(MainActivity.java:108)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at com.example.testjsons22.MainActivity$HttpAsyncTask.doInBackground(MainActivity.java:1)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-05 12:24:32.735: E/AndroidRuntime(5859):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-05 12:24:32.735: E/AndroidRuntime(5859):     ... 4 more

推荐答案

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

The JSON is URL encoded. Try this:

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

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

I don't know where you are calling this AsyncTask, but the only other error I'm seeing that could return a Runtime is the static method not being initialised. Try moving the code inside the AsyncTask.

这篇关于获取json可在localhost和某些网站上使用,但不能在我的真实服务器上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆