-1

I've build an AsyncTask and want to start another Activity when it ends (onPostExecute) but it won't work and I can't find the problem. Maybe there is a problem with String, String, String ? Here is my code:

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

    private Activity activity;

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

        StringBuilder sb = new StringBuilder();
        String http = "https://list-creater-service.herokuapp.com/api/v1/search";
        HttpURLConnection urlConnection = null;
        JSONObject json = null;
        HttpResponse response = null;

        try {
            //connect to server
            URL url = new URL(http);
            urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setDoOutput(true);
            urlConnection.setRequestMethod("POST");
            urlConnection.setUseCaches(false);
            urlConnection.setConnectTimeout(10000);
            urlConnection.setReadTimeout(10000);
            urlConnection.setRequestProperty("Content-Type","application/json");

            urlConnection.setRequestProperty("Host", "list-creater-service.herokuapp.com");
            urlConnection.connect();

            //Create JSONObject here
            JSONObject jsonParam = new JSONObject();
            jsonParam.put("gamemode", gametype);
            jsonParam.put("country", selCountry);
            jsonParam.put("min_size", minSize);
            jsonParam.put("max_size", maxSize);
            OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
            out.write(jsonParam.toString());
            out.close();



            int HttpResult = urlConnection.getResponseCode();
            if(HttpResult == HttpURLConnection.HTTP_OK){

                BufferedReader br = new BufferedReader(new InputStreamReader(
                        urlConnection.getInputStream(),"utf-8"));
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                String jsonS = sb.toString();

                JSONArray jsonArray = new JSONArray(jsonS);
                int length = jsonArray.length();

                String[] names = new String[length];

                for (int i = 0; i < length; i++) {
                    JSONObject jsonObject = new JSONObject(jsonArray.get(i).toString());

                    ArrayList serverList = new ArrayList();
                    serverList.add(jsonObject.getString("name"));

                    serverData = getSharedPreferences(filename, 0);
                    SharedPreferences.Editor editor = serverData.edit();
                    Set<String> set = new HashSet<String>();
                    set.addAll(serverList);
                    editor.putStringSet("name", set);
                    editor.commit();
                    System.out.println(jsonObject.getString("name"));

                    names[i] = jsonObject.getString("name");
                }

                //String name = jsonObject.getString("name");
                System.out.println("" + sb.toString());

            }else{
                System.out.println(urlConnection.getResponseMessage());
            }

        } catch (MalformedURLException e) {

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

            e.printStackTrace();
        } catch (JSONException e) {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(urlConnection!=null)
                urlConnection.disconnect();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        activity.startActivity(new Intent(activity, ServerIndex.class));
    }

}

Need some help! Thx :)

Yannik Pieper
  • 63
  • 1
  • 11

2 Answers2

1

You have Activity activity declared , but you have not assigned your current activity context to it . Assign the current activity context to it. Like
activity=currentContext;

0

You should use runOnUIThread in your onPostExecute

            runOnUiThread(new Runnable() { 

            public void run() {
                activity.startActivity(new Intent(activity,  ServerIndex.class));
            }});

And as Raghunandan said, activity is not initialized. You can access the startActivity method from your StartSearch class by using

 SomeActivity.this.startActivity  (SomeActivity has to be initialized)

And since onPostExecute runs on the UI thread, you may or may not call runOnUIThread.

dsp_user
  • 2,061
  • 2
  • 16
  • 23