我是Android开发的新手 我正在关注在线教程 目前我正在学习线程。
我尝试在以下代码中进行评论和烘烤以找到我的错误
我猜错误在newtype
行
如果我评论这一行和while循环,应用程序运行顺利。
请帮忙。
inputStream = connection.getInputStream();
logcat的:
package com.example.vivzmultithreadingexample;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener {
private EditText editText;
private ListView listView;
private String[] listofImages;
private ProgressBar progessBar;
private LinearLayout loadingSection = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
listView = (ListView) findViewById(R.id.urlList);
listView.setOnItemClickListener(this);
listofImages = getResources().getStringArray(R.array.imageUrls);
progessBar = (ProgressBar) findViewById(R.id.downloadProgress);
}
public void downloadImage(View v){
//Toast.makeText(this, "button Clicked", Toast.LENGTH_SHORT).show();
Thread myThread = new Thread(new DownloadImagesThread());
myThread.run();
}
public boolean downloadImageUsingThread(String url){
boolean sucessful = false;
URL downloadUrl = null;
HttpURLConnection connection= null;
InputStream inputStream = null;
try {
downloadUrl = new URL(url);
connection =(HttpURLConnection) downloadUrl.openConnection();
inputStream = connection.getInputStream();
int read = -1;
while((read =inputStream.read()) !=-1){
L.m(""+read);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
L.m(""+e);
} catch (IOException e) {
// TODO Auto-generated catch block
L.m(""+e);
}finally{
if(connection != null){
connection.disconnect();
}
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
L.m(""+e);
// TODO Auto-generated catch block
}
}
}
Toast.makeText(this, connection.toString(), Toast.LENGTH_SHORT).show();
return sucessful;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
editText.setText(listofImages[arg2]);
}
public class DownloadImagesThread implements Runnable {
public void run() {
downloadImageUsingThread(listofImages[0]);
editText.setText(listofImages[0]);
}
}
}
答案 0 :(得分:1)
您正在使用myThread.run()
来调用run()
方法,这将在调用它的线程中执行run()
,在您的情况下,它是MainThread且Android不允许网络访问MainThread。您必须调用myThread.start()
来声明执行您的线程。
答案 1 :(得分:0)
尝试在此行下使用这些语句
connection =(HttpURLConnection) downloadUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
答案 2 :(得分:0)
您正在主线程中调用网络呼叫,为此使用异步任务。 使用此代码..
DownLoadImageTask downloadImageUsingThread =new DownLoadImageTask ();
downloadImageUsingThread.execute(listofImages[0]);
并使用所有代码在doyntask的doInBackground(String ... params)方法中下载图像,并在post方法中设置文本。
class DownLoadImageTask extends Asyntask<String,String,String>{
@覆盖
protected String doInBackground(String... params) {
//所有代码都是她的
}
@Override
protected void onPostExecute(String result1) {
//在此处设置文字
}
}
enter code here
答案 3 :(得分:0)
找到解决方案。刚刚将myThread.run()
更改为myThread.start()