Java 调用异步任务类不起作用
我肯定我做错了什么,Android新手,我已经将URL存储在for循环中的字符串中,需要使用AsyncTask从该URL获取图像。对安卓系统来说很陌生,所以我遇到了一些问题。感谢您的帮助 SecondClass.javaJava 调用异步任务类不起作用,java,android,android-asynctask,Java,Android,Android Asynctask,我肯定我做错了什么,Android新手,我已经将URL存储在for循环中的字符串中,需要使用AsyncTask从该URL获取图像。对安卓系统来说很陌生,所以我遇到了一些问题。感谢您的帮助 SecondClass.java package edu.colum.iam.JSON; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStr
package edu.colum.iam.JSON;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
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 org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class SecondClass extends Activity {
private TextView first, second, third;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
first = (TextView)findViewById(R.id.firsttv);
second = (TextView)findViewById(R.id.secondtv);
third = (TextView)findViewById(R.id.thirdtv);
String id = getIntent().getStringExtra("id");
System.out.println(id);
String response = readBuilding(id.trim());
System.out.println(response);
try {
JSONObject jsonObj = new JSONObject(response);
if(jsonObj.length()>0)
{
String CourseName = jsonObj.getString("CourseName");
String CourseNumber = jsonObj.getString("CourseNumber");
String CourseDescription = jsonObj.getString("CourseDescription");
JSONArray arrayOfImages = jsonObj.getJSONArray("Images");
String theImage = arrayOfImages.get(0).toString(); //getting first image in the array and returning the link as a string
int arrSize = arrayOfImages.length();
List<String> urlOfImage = new ArrayList<String>(arrSize);
first.setText("CourseName:- "+CourseName);
second.setText("CourseNumber:- "+CourseNumber);
third.setText("CourseDescription:- "+CourseDescription);
for(int i = 0; i < arrayOfImages.length(); ++i)
{
theImage = arrayOfImages.get(i).toString();
urlOfImage.add(theImage);
ImageDownloadTask(theImage);
}
}
} catch (Exception e){
e.printStackTrace();
}
}
public String readBuilding(String id)
{
return postJSON("http://iam.colum.edu/portfolio/api/course/"+id+"?json=True");
}
private String postJSON(String stringURL) {
StringBuilder builder = new StringBuilder();
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(stringURL);
try {
httpget.addHeader("Content-Type","application/json; charset=utf-8");
HttpResponse response = httpclient.execute(httpget);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
}
包edu.colum.iam.JSON;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.StatusLine;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.Bundle;
导入android.widget.TextView;
导入android.widget.Toast;
公共类SecondClass扩展活动{
私有文本视图第一、第二、第三;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
first=(TextView)findViewById(R.id.firsttv);
second=(TextView)findViewById(R.id.secondtv);
第三个=(TextView)findViewById(R.id.thirdtv);
字符串id=getIntent().getStringExtra(“id”);
系统输出打印项次(id);
字符串响应=readBuilding(id.trim());
System.out.println(响应);
试一试{
JSONObject jsonObj=新的JSONObject(响应);
if(jsonObj.length()>0)
{
字符串CourseName=jsonObj.getString(“CourseName”);
字符串CourseNumber=jsonObj.getString(“CourseNumber”);
String CoursedDescription=jsonObj.getString(“CoursedDescription”);
JSONArray arrayOfImages=jsonObj.getJSONArray(“图像”);
String theImage=arrayOfImages.get(0).toString();//获取数组中的第一个图像并以字符串形式返回链接
int arrSize=arrayOfImages.length();
列表urlOfImage=新的ArrayList(arrSize);
第一个.setText(“CourseName:-”+CourseName);
second.setText(“CourseNumber:-”+CourseNumber);
第三,setText(“CourseDescription:-”+CourseDescription);
对于(int i=0;i
ImageDownloadTask.java
package edu.colum.iam.JSON;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
public class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> {
/** The url from where to download the image. */
private String url;
/** Reference to the view which should receive the image */
private final WeakReference<ImageView> imageRef;
/**
* Constructor.
*
* @param imageView
* The ImageView which will receive the image.
*/
public ImageDownloadTask(ImageView imageView) {
imageRef = new WeakReference<ImageView>(imageView);
}
/**
* This function will be executed to download the image in a background
* process.
*
*/
@Override
protected Bitmap doInBackground(String... params) {
try {
InputStream in = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
return bitmap;
} catch (Exception e) {
Log.e("ImageDownload", e.getMessage());
}
return null;
}
/**
* This function will be called after the image download and attaches
* the bitmap to the ImageView.
*
*/
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageRef != null) {
ImageView imageView = imageRef.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
包edu.colum.iam.JSON;
导入java.io.InputStream;
导入java.lang.ref.WeakReference;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.os.AsyncTask;
导入android.util.Log;
导入android.widget.ImageView;
公共类ImageDownloadTask扩展了AsyncTask{
/**从何处下载图像的url*/
私有字符串url;
/**引用应接收图像的视图*/
私有最终WeakReference imageRef;
/**
*构造器。
*
*@param-imageView
*将接收图像的ImageView。
*/
公共图像下载任务(图像视图图像视图){
imageRef=新的WeakReference(imageView);
}
/**
*将执行此功能以在后台下载图像
*过程。
*
*/
@凌驾
受保护位图doInBackground(字符串…参数){
试一试{
InputStream in=new java.net.URL(URL.openStream();
位图位图=位图工厂.decodeStream(in);
返回位图;
}捕获(例外e){
Log.e(“ImageDownload”,e.getMessage());
}
返回null;
}
/**
*此函数将在图像下载和附加后调用
*将位图添加到ImageView。
*
*/
@凌驾
受保护的void onPostExecute(位图){
如果(isCancelled()){
位图=空;
}
如果(imageRef!=null){
ImageView=imageRef.get();
如果(imageView!=null){
设置图像位图(位图);
}
}
}
}
这里可能有更多的问题(例如可能在主线程上访问网络?),但这是图像下载任务(theImage)代码>实际上不会执行您的异步任务