我怀疑我在简单的 Android 应用程序中遇到了一些线程问题。
我有一个 Activity 执行以下操作:
- 在主线程上运行一个对话框
- 旋转一个后台线程并点击一个 PHP 脚本以使用
DefaultHttpClient
并关闭此连接来检索数据。这成功地返回了一个指向互联网上图片的 URL - 打开
HttpUrlConnection
调用conn
并尝试 conn.connect - 应用程序此时冻结,没有错误
不知道该放什么代码,我可以全部粘贴,但这太多了,所以如果需要更多信息,请告诉我:
/**
* Background Async Task to Load all Question by making HTTP Request
*/
class LoadAllImages extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
// dialog init'd here
}
@Override
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_question_details, "GET", params);
// Check your log cat for JSON response
Log.d("All Questions: ", json.toString());
try {
// images found
// Getting Array of questions
images = json.getJSONArray(TAG_IMAGES);
// looping through All questions
for (int i = 0; i < images.length(); i++) {
JSONObject c = images.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_IMAGEID);
String location = c.getString(TAG_IMAGELOCATION);
URL myFileUrl = null;
try {
myFileUrl = new URL(location);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect(); // freezes here
int length = conn.getContentLength();
int[] bitmapData = new int[length];
byte[] bitmapData2 = new byte[length];
InputStream is = conn.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}