json error in logcat file in aysnc task

public class MainActivity extends AppCompatActivity {
    public TextView textView;public Button clickMe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        clickMe = (Button)findViewById(R.id.clickMe);
        clickMe.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            new JsonClass().execute("www.webbanknepal.com/app/services.php");
        }});
    }



    class JsonClass extends AsyncTask<String,Void,String> {
        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection connection=null;
            BufferedReader reader=null;
            try {
                URL url= new URL(params[0]);
                connection=(HttpURLConnection)url.openConnection();
                connection.connect();
                InputStream stream=connection.getInputStream();
                InputStreamReader isr= new InputStreamReader(stream);
                reader =new BufferedReader(isr);
                StringBuffer buffer=new StringBuffer();
                String line="";
                while((line=reader.readLine())!=null)
                {
                    buffer.append(line);

                }
                return buffer.toString();



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

            } finally {
                if(connection!=null){ connection.disconnect();}
                try {
                    if(reader!=null){ reader.close();};
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            textView= (TextView)findViewById(R.id.textView);
            textView.setText(s.toString());
        }

    }

tHIS IS MY QUESTIOMN

i get application is unfortunately close here is my log cat file FATAL EXCEPTION

An error occured while executing doInBackground() at a.done(AsyncTask.java:300) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:841) Caused by: java.lang.NullPointerException at com.example.shailendra.fulljson.MainActivity$JsonClass.doInBackground(MainActivity.java:81) at com.example.shailendra.fulljson.MainActivity$JsonClass.doInBackground(MainActivity.java:47) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExec utor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:841)


I change little bit on your code and it working fine. Check the code....

    public class MainActivity extends AppCompatActivity { public    TextView textView;public Button clickMe;@Override
         protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
             clickMe= (Button)findViewById(R.id.clickMe);clickMe.setOnClickListener(
             new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                    new JsonClass().execute("http://www.webbanknepal.com/app/services.php");
                   }
             });
    }


   class JsonClass extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            InputStreamReader isr = new InputStreamReader(stream);
            reader = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);

            }
            return buffer.toString();


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

        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(s.toString());
    }

   }

}


You are passing null string for onPostExecute(String s).

Do this:

public class MainActivity extends AppCompatActivity             
 {           
   public TextView textView;          
   public Button clickMe;         
   public static String s;        
   @Override    
   protected void onCreate(Bundle savedInstanceState) 
   {      
    super.onCreate(savedInstanceState);      
    ...                                 
    ...  
    new JsonClass().execute("www.webbanknepal.com/app/services.php",null,s);      
    }});       
   }
   class JsonClass extends AsyncTask<String,Void,String>
   {  @Override
     protected String doInBackground(String... params) {
     ...
     ...
     String line="";
            while((line=reader.readLine())!=null)
            {
                buffer.append(line);

            }
            s=buffer.toString();
            return s;
链接地址: http://www.djcxy.com/p/78426.html

上一篇: java.lang.RuntimeException:执行doInBackground时发生错误(

下一篇: aysnc任务中logcat文件中的json错误