0

I want use JSON and take data form web. And I find a tutorial and I applied whatever. The result was frustration. Briefly:

FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occurred while executing doInBackground()

This is my code:

package com.example.winsekiz.searchlist;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;

public class WebServer extends Activity {


    final static String URI = "www.hocaogluticaret.com/a.html";
    TextView tvData;
    JSONObject json;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.turkey);
        tvData = (TextView) findViewById(R.id.tv);
        new Game().execute("text");
    }

    protected JSONObject readGameParks() throws ClientProtocolException, IOException, JSONException{

        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(URI);
        HttpResponse response =client.execute(get);
        StatusLine status = response.getStatusLine();
        int s = status.getStatusCode();

        if(s == 200) {

            HttpEntity e = response.getEntity();
            String data = EntityUtils.toString(e);
            JSONArray posts = new JSONArray(data);
            JSONObject last = posts.getJSONObject(0);
        }

        return  null;
    }

    public class Game extends AsyncTask<String, String, String>{

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

            try{
                json = readGameParks();
                String data = json.getString(params[0]);
                return  data;
            }

            catch (ClientProtocolException e) {
                e.printStackTrace();
            }

            catch (IOException e) {
                e.printStackTrace();
            }

            catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String data){
            tvData.setText(data);
        }
    }
}

And this is error:

03-21 22:29:17.110 10922-10922/com.example.winsekiz.searchlist I/art: Not late-enabling -Xcheck:jni (already on)
03-21 22:29:17.120 10922-10922/com.example.winsekiz.searchlist W/System: ClassLoader referenced unknown path: /data/app/com.example.winsekiz.searchlist-2/lib/x86_64
03-21 22:29:17.130 10922-10935/com.example.winsekiz.searchlist D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-21 22:29:17.200 10922-10935/com.example.winsekiz.searchlist I/OpenGLRenderer: Initialized EGL, version 1.4
03-21 22:29:17.270 10922-10935/com.example.winsekiz.searchlist W/EGL_emulation: eglSurfaceAttrib not implemented
03-21 22:29:17.270 10922-10935/com.example.winsekiz.searchlist W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7feb1eea0e40, error=EGL_SUCCESS
03-21 22:29:18.910 10922-10944/com.example.winsekiz.searchlist E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                                 Process: com.example.winsekiz.searchlist, PID: 10922
                                                                                 java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                     at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                                     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                     at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                     at java.lang.Thread.run(Thread.java:818)
                                                                                  Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=www.hocaogluticaret.com/a.html
                                                                                     at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:603)
                                                                                     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:299)
                                                                                     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
                                                                                     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
                                                                                     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
                                                                                     at com.example.winsekiz.searchlist.WebServer.readGameParks(WebServer.java:47)
                                                                                     at com.example.winsekiz.searchlist.WebServer$Game.doInBackground(WebServer.java:68)
                                                                                     at com.example.winsekiz.searchlist.WebServer$Game.doInBackground(WebServer.java:62)
                                                                                     at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                                     at java.lang.Thread.run(Thread.java:818) 
03-21 22:29:18.990 10922-10935/com.example.winsekiz.searchlist W/EGL_emulation: eglSurfaceAttrib not implemented
03-21 22:29:18.990 10922-10935/com.example.winsekiz.searchlist W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7feb1d9ff2c0, error=EGL_SUCCESS
03-21 22:29:19.010 10922-10935/com.example.winsekiz.searchlist E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7feb2e2daa90
03-21 22:29:24.090 10922-10935/com.example.winsekiz.searchlist E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7feb2e2daf60
03-21 22:34:18.920 10922-10944/? I/Process: Sending signal. PID: 10922 SIG: 9
2
  • i would suggest using volley for API calls Commented Mar 21, 2016 at 20:52
  • Try to clean and rebuild you project. Also make sure you have added the <uses-permission android:name="android.permission.INTERNET" /> to your manifest Commented Mar 21, 2016 at 20:54

2 Answers 2

1

Lets step though the code

1) Start the AsyncTask

new Game().execute("text");

2) Goes to doInBackground

json = readGameParks();

3) Connects to a non-qualified URI

HttpGet get = new HttpGet("www.hocaogluticaret.com/a.html");

and you get an error, to fix that put http:// in front of the address.


4) Assuming a connection was made appropriately, continue on to return null;

String data = json.getString(params[0]);
return  data;

Here, params[0] is "text", but json is null, therefore throwing a NullPointerException.

To fix, try not returning null in readGameParks. I think you should do return last; inside the status code check.


In code,

JSONObject last = new JSONObject();

if(s == 200) {
    HttpEntity e = response.getEntity();
    String data = EntityUtils.toString(e);
    JSONArray posts = new JSONArray(data);
    last = posts.getJSONObject(0);
}

return last;
Sign up to request clarification or add additional context in comments.

3 Comments

With 3. step, so much errors corrected. And now, I have only one error: "E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7feb2e2da5c0" And Virtual Device is showing blank page. And to be honest, I dont understand others.
Like I told you in the other comment. Look for Caused by and read What is a Null Pointer Exception, and how do I fix it?
The problem is NullPointerException. indirectly "return last;" I fix it with u. Thank u a lot.
1

Your url is not complete.

www.hocaogluticaret.com/a.html

should be

http://www.hocaogluticaret.com/a.html

or

https://www.hocaogluticaret.com/a.html

5 Comments

While this is one problem. That is not what the error refers to. readGameParks is returning null
Okey. Error codes are now only one. And Virtual Device showing blank page. Error code is: "getSlotFromBufferLocked: unknown buffer."
Fully error messages are: emulation: eglSurfaceAttrib not implemented W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7feb1d9be1c0, error=EGL_SUCCESS E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7feb2e2da9b0
@EnesCeylan Look for the words "Caused By". The text you just pasted is not related to the error that was actually caused.
It might be best to just make a new question, for this, because it seems this is a different problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.