gpt4 book ai didi

java - 长度无法解析或不是字段

转载 作者:行者123 更新时间:2023-11-30 03:56:34 25 4
gpt4 key购买 nike

我是 Java 和 Android 的新手。

我的程序一直出现这个错误length cannot be resolved or is not a field 我只是不明白如何解决这个问题。这是代码

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

最佳答案

公开 URL

public static final String[] URLS = {

并将其用作

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

试一试..

关于java - 长度无法解析或不是字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13245375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com