Getting json works on localhost and some websites, but not working on my real server

拟墨画扇 提交于 2019-12-13 08:58:52

问题


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

The application is working on localhost can get the json, But I can't have access to the website that is on server.

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

HttpAsyncTask class:

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

These are the errors:

 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

回答1:


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
}

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.



来源:https://stackoverflow.com/questions/28874205/getting-json-works-on-localhost-and-some-websites-but-not-working-on-my-real-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!