到來時,下載的文件在我的Android應用程序,當我嘗試下載圖像文件從Amazon S3這讓我異常安卓:異常來自AmazonS3
這裏是例外:
08-30 11:20:34.157: W/System.err(6519): android.os.NetworkOnMainThreadException
08-30 11:20:34.157: W/System.err(6519): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
08-30 11:20:34.157: W/System.err(6519): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-30 11:20:34.157: W/System.err(6519): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-30 11:20:34.157: W/System.err(6519): at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-30 11:20:34.157: W/System.err(6519): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
08-30 11:20:34.168: W/System.err(6519): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-30 11:20:34.168: W/System.err(6519): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-30 11:20:34.168: W/System.err(6519): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-30 11:20:34.177: W/System.err(6519): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-30 11:20:34.177: W/System.err(6519): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-30 11:20:34.177: W/System.err(6519): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-30 11:20:34.177: W/System.err(6519): at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:285)
08-30 11:20:34.177: W/System.err(6519): at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:167)
08-30 11:20:34.187: W/System.err(6519): at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:2781)
08-30 11:20:34.187: W/System.err(6519): at com.amazonaws.services.s3.AmazonS3Client.getObject(AmazonS3Client.java:872)
08-30 11:20:34.187: W/System.err(6519): at com.example.amazons3.AmazonS3DemoActivity.Download(AmazonS3DemoActivity.java:74)
08-30 11:20:34.187: W/System.err(6519): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 11:20:34.187: W/System.err(6519): at java.lang.reflect.Method.invoke(Method.java:511)
08-30 11:20:34.187: W/System.err(6519): at android.view.View$1.onClick(View.java:3039)
08-30 11:20:34.197: W/System.err(6519): at android.view.View.performClick(View.java:3511)
08-30 11:20:34.197: W/System.err(6519): at android.view.View$PerformClick.run(View.java:14105)
08-30 11:20:34.197: W/System.err(6519): at android.os.Handler.handleCallback(Handler.java:605)
08-30 11:20:34.197: W/System.err(6519): at android.os.Handler.dispatchMessage(Handler.java:92)
08-30 11:20:34.207: W/System.err(6519): at android.os.Looper.loop(Looper.java:137)
08-30 11:20:34.207: W/System.err(6519): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-30 11:20:34.207: W/System.err(6519): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 11:20:34.207: W/System.err(6519): at java.lang.reflect.Method.invoke(Method.java:511)
08-30 11:20:34.217: W/System.err(6519): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-30 11:20:34.227: W/System.err(6519): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-30 11:20:34.227: W/System.err(6519): at dalvik.system.NativeStart.main(Native Method)
這我java代碼:
public class AmazonS3Activity extends Activity
{
private String Tag = "Downlaod";
Button btnDownload;
Context myContext;
String product_code = "pro_code";
String bucketName = "bucketnamw";
String appDirPath = Environment.getExternalStorageDirectory().getName() +"/foldername/";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myContext = this;
btnDownload = (Button) findViewById(R.id.btnDownload);
}
public void Download(View button)
{
try
{
String imgName = product_code+"1.jpg";
String md5Name = getMD5HasCode(product_code);
String productCode = md5Name+".app/";
String filePath = getFullFilePath(imgName);
String accessKey = "accessKey";
String secretKey = "secretKey";
AWSCredentials credential = new BasicAWSCredentials(accessKey, secretKey);
Log.e(Tag, "FileName : " + filePath, null);
String strObjectKey = productCode + imgName.replace(product_code,md5Name);
Log.e(Tag, "Object Key : " + strObjectKey, null);
GetObjectRequest objRequest = new GetObjectRequest(bucketName,strObjectKey);
AmazonS3Client myS3Client = new AmazonS3Client(credential);
S3Object s3Object = myS3Client.getObject(objRequest);
long fileSize = s3Object.getObjectMetadata().getContentLength();
final InputStream input = s3Object.getObjectContent();
final FileOutputStream fos = new FileOutputStream(filePath);
long total = 0;
int len=0;
byte[] buf = new byte[1024];
while((len = input.read(buf)) > 0)
{
fos.write(buf,0,len);
total = total + len;
}
if(len == -1 && total==fileSize)
{
Thread.currentThread().interrupt();
input.close();
fos.close();
}
}
catch (Exception e)
{
e.printStackTrace();
Log.e(Tag, "Error Key : " + e.getMessage(), null);
}
}
public String getMD5HasCode(String strMessage)
{
try
{
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] messageDigestBytes = messageDigest.digest(strMessage.getBytes());
BigInteger hasNumber = new BigInteger(1,messageDigestBytes);
String md5 = hasNumber.toString(16);
while(md5.length()<32)
{
md5 = "0" + md5;
}
return md5;
}
catch (Exception ex)
{
Toast.makeText(myContext, "ERROR in HasCode : "+ex.toString(), Toast.LENGTH_SHORT).show();
return null;
}
}
public String getFullFilePath(String filename)
{
File dir = new File(appDirPath);
if(!dir.exists())
{
dir.mkdir();
}
return appDirPath+filename;
}
}
那是因爲你的N/W活動是在主線程和你在一個嚴格的模式策略不能使用像上面2.3嘗試下載轉移到異步任務 –
由於它工作在異步任務。但是,同樣的代碼,當我試圖在平板電腦上運行它給我的錯誤,如:請求時間和當前時間之間的差異過大 – Mansi
@Mansi你有這樣一個完整的代碼示例或教程?我會很高興,如果你爲此做了一個教程..感謝和更多的權力^ _^ – Cjames