2015-12-08 80 views
-4

我檢查了它,每次我試圖運行項目 ,使我的應用程序崩潰... 堆棧在while循環.. 和don來自環路Android,我的程序崩潰,因爲那停留在while循環

「噸退出主代碼:

package com.example.omermalka.jasonparsingdemo; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.TextView; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.net.ssl.HttpsURLConnection; 

public class MainActivity extends AppCompatActivity { 
    private static final String TAG = "com.example.omermalka.jasonparsingdemo"; 
    private TextView tvData; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
     Log.i(TAG, "1"); 

     Button btnHit = (Button)findViewById(R.id.btnHit); 
     tvData = (TextView)findViewById(R.id.tvJsonItem); 
     Log.i(TAG,"2"); 
     btnHit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       new JsonTask().execute("http://jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt"); 
       Log.i(TAG,"2"); 

      } 
     }); 
    } 



    public class JsonTask extends AsyncTask<String,String,String> 
    { 
     @Override 
     protected String doInBackground(String... params) { 
      Log.i(TAG,"3"); 

      //connecting to the Server 
      HttpsURLConnection connection = null; 
      BufferedReader reader = null; 
      Log.i(TAG,"3.1"); 

      try { 
       URL url = new URL(params[0]); 
       Log.i(TAG, params[0]+""); 
       connection = (HttpsURLConnection) url.openConnection(); 
       Log.i(TAG, "3.15"); 
       connection.connect(); 
       Log.i(TAG, "3.2"); 


       InputStream stream = connection.getInputStream(); 
       //that will take the strem from the connection and 
       //stoage that on the stream object 

       reader = new BufferedReader(new InputStreamReader(stream)); 
       StringBuffer buffer = new StringBuffer(); 

       String line = reader.readLine(); 
       Log.i(TAG,"4"); 

       //the line is read the line from the Buffer Reader 
       //and add it to the StringBuffer 
       while ((line != null)) { 
        Log.i(TAG,"5"); 
        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 result) { 

       super.onPostExecute(result); 
       tvData.setText(result); 

      } 


    } 
/* 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    }*/ 
} 

錯誤日誌 :

12-08 05:06:05.854 16934-16990/com.example.omermalka.jasonparsingdemo E/dalvikvm-heap: Out of memory on a 57283554-byte allocation. 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime: Process: com.example.omermalka.jasonparsingdemo, PID: 16934 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground() 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at android.os.AsyncTask$3.done(AsyncTask.java:300) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.lang.Thread.run(Thread.java:841) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime: Caused by: java.lang.OutOfMemoryError 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.lang.StringBuffer.append(StringBuffer.java:219) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at com.example.omermalka.jasonparsingdemo.MainActivity$JsonTask.doInBackground(MainActivity.java:95) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at com.example.omermalka.jasonparsingdemo.MainActivity$JsonTask.doInBackground(MainActivity.java:61) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at android.os.AsyncTask$2.call(AsyncTask.java:288) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)  
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)  
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)  
12-08 05:06:05.858 16934-16990/com.example.omermalka.jasonparsingdemo E/AndroidRuntime:  at java.lang.Thread.run(Thread.java:841)  

感謝所有的幫手!

+0

'ClassCastException異常:http.HttpURLConnectionImpl不能轉換到javax.net.ssl.HttpsURLConnection' – Blackbelt

+0

是什麼用意?我該怎麼辦 ? –

+1

使用'https:// jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt',如果支持http,那麼爲什麼不使用'HttpURLConnection'而不是'HttpsURLConnection'? –

回答