1

我是 Java 和 Android 的新手。

我的程序长度无法解决或者不是我只是不明白如何解决这个问题的字段。这是代码

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.os.Environment;

public class DownloadImages extends AsyncTask {


protected Object doInBackground(Object... params) {
    System.out.println("External Storage State = " + Environment.getExternalStorageState());
    File directory=new File(Environment.getExternalStorageDirectory(), "/Images");
    if (directory.exists()==false)
    {
        directory.mkdir();
    }
    for(int i = 0; i <URLS.length; i++) {
        try {
            File firstFile=new File(directory+"/" +i+ ".jpeg");
            if(firstFile.exists()==false)
            {
                HttpClient httpClient =new DefaultHttpClient();
                HttpGet httpGet =new HttpGet(URLS[i]);
                HttpResponse resp = httpClient.execute(httpGet);
                System.out.println("Status Code = " +resp.getStatusLine().getStatusCode());
                if(resp.getStatusLine().getStatusCode()==200)
                {
                    HttpEntity entity = resp.getEntity();
                    InputStream is = entity.getContent();
                    Boolean status = firstFile.createNewFile();

                    FileOutputStream foutS = new FileOutputStream(firstFile);
                    byte[] buffer = new byte[1024];
                    long total = 0;
                    int count;
                    while((count = is.read(buffer)) != -1){
                        total += count;
                        foutS.write(buffer, 0, count);
                    }
                    foutS.close();
                    is.close();
                    publishProgress(i);

                    }
                }

            }catch(MalformedURLException e){
                e.printStackTrace();
            }catch(ClientProtocolException e){
                e.printStackTrace();
            }catch(IOException e){
                e.printStackTrace();
            }
    }
    return null;
        }

@SuppressWarnings("unchecked")
protected void onProgressUpdate(Object... values){
    super.onProgressUpdate(values);

    }
}

在 For 语句行中出现错误

  for(int i = 0; i <URLS.length; i++) {

MainActivity.java

import android.os.Bundle;
 import android.app.Activity;
 import android.view.Menu;

 public class MainActivity extends Activity{

private static final String[] URLS = {
    "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_2851.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_2944.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_2989.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3005.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3012.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3034.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3047.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3092.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3110.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3113.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3128.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3160.jpg",
};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new DownloadImages().execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}
4

2 回答 2

1

将 URL 设为公开

public static final String[] URLS = {

并将其用作

for(int i = 0; i <MainActivity.URLS.length; i++) {

尝试一下..,。

于 2012-11-06T06:28:15.153 回答
0

内部类是在另一个类中声明的类。试着DownloadImages放进去MainActivity

于 2012-11-06T06:24:01.830 回答