Android,我的程序崩溃了,因为它卡在了while循环中
我检查了它,每次我试图运行该项目 使我的应用程序崩溃。。。 while循环上的堆栈。。 不要退出循环 主要代码:Android,我的程序崩溃了,因为它卡在了while循环中,android,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.
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);
}*/
}
谢谢所有的帮助 将HttpsURLConnection更改为HttpURLConnection 因为你的链接是 并将readLine()置于while条件下
while (line = reader.readLine()) != null) {
Log.i(TAG,"5");
buffer.append(line);
}
ClassCastException:http.HttpURLConnectionImpl不能强制转换为javax.net.ssl.HttpsURLConnection
这是什么意思?我应该怎么做?使用https://jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt
如果支持http,为什么不使用HttpURLConnection
而不是HttpsURLConnection
?我理解。。泰,我有更多的错误,我的循环不是最终的,她不会停止,直到它使应用程序crash@omermalka:哪个环路?
while (line = reader.readLine()) != null) {
Log.i(TAG,"5");
buffer.append(line);
}