aysnc任务中logcat文件中的json错误

时间:2016-10-03 05:06:45

标签: android json

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

    }

这是我的问题

我得到应用程序很遗憾,这里关闭的是我的日志cat文件 致命的例外

  

执行doInBackground()时发生错误                                                                                      at a.done(AsyncTask.java:300)                                                                                      在   java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)                                                                                      at java.util.concurrent.FutureTask.setException(FutureTask.java:222)                                                                                      在java.util.concurrent.FutureTask.run(FutureTask.java:242)                                                                                      在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)                                                                                      在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)                                                                                      在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)                                                                                      在java.lang.Thread.run(Thread.java:841)                                                                                   引起:java.lang.NullPointerException                                                                                      在   com.example.shailendra.fulljson.MainActivity $ JsonClass.doInBackground(MainActivity.java:81)                                                                                      在   com.example.shailendra.fulljson.MainActivity $ JsonClass.doInBackground(MainActivity.java:47)                                                                                      在android.os.AsyncTask $ 2.call(AsyncTask.java:288)                                                                                      在java.util.concurrent.FutureTask.run(FutureTask.java:237)                                                                                      在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)                                                                                      在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)                                                                                      在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)                                                                                      在java.lang.Thread.run(Thread.java:841)

2 个答案:

答案 0 :(得分:1)

我在代码上稍微改变一下,它运行正常。检查代码......

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

   }

}

答案 1 :(得分:0)

您正在为 onPostExecute(String s)传递空字符串

这样做:

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;