0

Im trying to get data from my database , but it comes to a crash in my App. I already tried to debug it step by step and found out that this line is going to crash

jArray = new JSONArray(result);

Here is my whole Code :

   public class get_data extends AsyncTask<Void, Void, Boolean>
    {

        @Override
        protected Boolean doInBackground(Void... params) {

            String result = null;
            InputStream is = null;
            StringBuilder sb = null;
            // http post
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://example.com/output.php");
                // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection" + e.toString());
            }
            // convert response to string
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);
                sb = new StringBuilder();
                sb.append(reader.readLine() + "\n");
                String line = "0";
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }
            // paring data
            JSONArray jArray;
            try {
                jArray = new JSONArray(result);
                JSONObject json_data = null;
                ct_name = new String[jArray.length()];
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    ct_id = json_data.getInt("A_ID");
                    ct_name[i] = json_data.getString("A_USERNAME");
                }
            } catch (JSONException e1) {
                Toast.makeText(getBaseContext(), "No output", Toast.LENGTH_LONG)
                        .show();
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
            ausgabe.setText(ct_name[0]);
            return null;
        }
    }

Im calling it in onCreate :

new get_data().execute();

Report :

11-22 12:31:18.762: E/AndroidRuntime(376): FATAL EXCEPTION: AsyncTask #1
11-22 12:31:18.762: E/AndroidRuntime(376): Process: com.JewE.engoneengine, PID: 376
11-22 12:31:18.762: E/AndroidRuntime(376): java.lang.RuntimeException: An error occured while executing doInBackground()
11-22 12:31:18.762: E/AndroidRuntime(376):  at android.os.AsyncTask$3.done(AsyncTask.java:300)
11-22 12:31:18.762: E/AndroidRuntime(376):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
11-22 12:31:18.762: E/AndroidRuntime(376):  at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
11-22 12:31:18.762: E/AndroidRuntime(376):  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-22 12:31:18.762: E/AndroidRuntime(376):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-22 12:31:18.762: E/AndroidRuntime(376):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-22 12:31:18.762: E/AndroidRuntime(376):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-22 12:31:18.762: E/AndroidRuntime(376):  at java.lang.Thread.run(Thread.java:841)
11-22 12:31:18.762: E/AndroidRuntime(376): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-22 12:31:18.762: E/AndroidRuntime(376):  at android.os.Handler.<init>(Handler.java:200)
11-22 12:31:18.762: E/AndroidRuntime(376):  at android.os.Handler.<init>(Handler.java:114)
11-22 12:31:18.762: E/AndroidRuntime(376):  at android.widget.Toast$TN.<init>(Toast.java:388)
11-22 12:31:18.762: E/AndroidRuntime(376):  at android.widget.Toast.<init>(Toast.java:114)
11-22 12:31:18.762: E/AndroidRuntime(376):  at android.widget.Toast.makeText(Toast.java:273)
11-22 12:31:18.762: E/AndroidRuntime(376):  at com.JewE.engoneengine.java_get_data$get_data.doInBackground(java_get_data.java:90)
11-22 12:31:18.762: E/AndroidRuntime(376):  at com.JewE.engoneengine.java_get_data$get_data.doInBackground(java_get_data.java:1)
11-22 12:31:18.762: E/AndroidRuntime(376):  at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-22 12:31:18.762: E/AndroidRuntime(376):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-22 12:31:18.762: E/AndroidRuntime(376):  ... 4 more

The PHP is correct Im getting my json data :

[{"A_ID":"1","A_USERNAME":"johann","A_PASSWORD":"joejoe"}]

3 Answers 3

1

Use The async task shown below and do not forget to include permissions in Android manifest

<uses-permission android:name="android.permission.INTERNET" />

Use this for Async task In Android:

To call Async Task Use this from any method:

List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("key1", "value1"));
    params.add(new BasicNameValuePair("key1", "value2"));
    new WEBSERVICEREQUESTOR(URL, params).execute();

Make this as member of class :

class WEBSERVICEREQUESTOR extends AsyncTask<String, Integer, String>
{

    String URL;
    List<NameValuePair> parameters;

    private ProgressDialog pDialog;

    public WEBSERVICEREQUESTOR(String url, List<NameValuePair> params)
    {
        this.URL = url;
        this.parameters = params;
    }

    @Override
    protected void onPreExecute()
    {
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Processing Request...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params)
    {
        try
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            HttpPost httpPost = new HttpPost(URL);

            if (parameters != null)
            {
                httpPost.setEntity(new UrlEncodedFormEntity(parameters));
            }
            httpResponse = httpClient.execute(httpPost);

            httpEntity = httpResponse.getEntity();
            return EntityUtils.toString(httpEntity);

        }  catch (Exception e)
        {

        }
        return "";
    }

    @Override
    protected void onPostExecute(String result)
    {
        pDialog.dismiss();

        try
        {

        } catch (Exception e)
        {

        }
        super.onPostExecute(result);
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

You could try doing the following instead of constructing and calling your AsyncTask directly:

new Handler(Looper.getMainLooper()).post(new Runnable() {

    public void Run() { 
           new get_data().execute(); 
    }
});

This is based on the answer provided by user Timo Ohr, which can be found here.

Comments

0

I got it !

I just forgot to delete the html tags in my php script :) Now I just have sth like this :

  <?php
    ...
    ?>

That was it ! Hopefully I can help someone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.