package com.Itattooz.Gallery;
@SuppressWarnings("unused")
public class grid_layout extends Activity {
private GridView list;
private String id_folder;
private LazyAdapter1 adapter;
private Intent intent;
private String main_folder;
private JSONArray jArray;
private String result = null;
private InputStream is = null;
private StringBuilder sb = null;
private String[] r;
boolean flag1 = false, flag2 = false, flag3 = false;
private String[] sub_folder_id;
private String[] path;
private String[] sub_folder_name;
private String[] flag;
private String previouse_folder;
private String[] url;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
intent = getIntent();
main_folder = intent.getStringExtra("selected_item");
new Thread(new Runnable() {
public void run() {
databaseConnectivity();
}
}).start();
list = (GridView) findViewById(R.id.list);
adapter = new LazyAdapter1(this, url, sub_folder_name);
list.setAdapter(adapter);
list.setOnItemClickListener(grid_listener);
}
@Override
public void onDestroy() {
list.setAdapter(null);
super.onDestroy();
}
// djhwawd
OnItemClickListener grid_listener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
id_folder = sub_folder_id[position];
flag1 = true;
TextView text = (TextView) v.findViewById(R.id.text);
String str = (String) text.getText();
if (flag[position].equals("X")) {
//main_folder = main_folder + "/" + str.replace(" ", "%20");
// int flags = Intent.FLAG_ACTIVITY_SINGLE_TOP;
intent = new Intent(v.getContext(), grid_layout_main.class);
intent.putExtra("folder_name", main_folder.replace(" ", "%20") + "/" + str.replace(" ", "%20"));
intent.putExtra("id", sub_folder_id[position]);
startActivity(intent);
} else {
flag2=true;
main_folder = main_folder + "/" + str.replace(" ", "%20");
int flags = Intent.FLAG_ACTIVITY_SINGLE_TOP;
intent = new Intent(v.getContext(), grid_layout.class);
intent.setFlags(flags);
intent.putExtra("selected_item", main_folder);
startActivity(intent);
}
}
};
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
new Thread(new Runnable() {
public void run() {
databaseConnectivity();
}
}).start();
adapter = new LazyAdapter1(this, url, sub_folder_name);
list.setAdapter(adapter);
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
list.setOnItemClickListener(grid_listener);
}
public void databaseConnectivity() {
HttpPost httppost = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
HttpClient httpclient = new DefaultHttpClient();
if (flag1 == false) {
nameValuePairs
.add(new BasicNameValuePair("folder", main_folder));
httppost = new HttpPost(
"http://www.itattooz.com/android/index.php");
} else if (flag1 == true) {
nameValuePairs.add(new BasicNameValuePair("sub_folder_id",
id_folder));
httppost = new HttpPost(
"http://www.itattooz.com/android/index2.php");
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
String rt;
try {
jArray = new JSONArray(result);
path = new String[jArray.length()];
sub_folder_id = new String[jArray.length()];
sub_folder_name = new String[jArray.length()];
flag = new String[jArray.length()];
url = new String[jArray.length()];
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
sub_folder_id[i] = json_data.getString("id");
sub_folder_name[i] = json_data.getString("folder");
flag[i] = json_data.getString("flag");
path[i] = json_data.getString("path");
rt = "http://www.itattooz.com/itattooz/"
+ main_folder.replace(" ", "%20") + "/"
+ sub_folder_name[i].replace(" ", "%20")
+ "/cover_image/" + path[i].replace(" ", "%20");
url[i] = rt;
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "No Image Found",
Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
onResume();
Log.d(this.getClass().getName(), "back button pressed");
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onResume() { // After a pause OR at startup
super.onResume();
// Refresh your stuff here
}
@Override
protected void onPause() {
super.onPause();
}
}
Hello Above是我的图库视图代码,作为网格布局..
我在网上研究了很多,并且知道我应该使用AsyncTask来实现我想要实现的目标。这里我遇到的问题很少
不知道我该怎么做才能摆脱这个问题..请帮忙......
答案 0 :(得分:0)
使用asyncTask
来处理此异常。
请参阅Android开发人员Link
或者您可以看到与此Link HERE
相关的stackoverflow接受的答案对于exmp
class YourTask extends AsyncTask<URL, Integer, String> {
protected String doInBackground(URL... urls) {
// Fetch Data (Task A)
return "Result";
}
protected void onProgressUpdate(Integer... progress) {
// Show progress
}
protected void onPostExecute(String result) {
// Show UI after complete (Task B)
}
}
答案 1 :(得分:0)
使用异步任务
或
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
答案 2 :(得分:0)
使用 AsyncTask
或
在线程开始使用以下代码禁用严格模式之前,在onCreate()方法中添加以下代码:
这不是解决方案,但避免主线程上的网络IO,所以我建议 AsyncTask
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
还要检查AndroidManifest.xml文件中的互联网权限
<uses-permission android:name="android.permission.INTERNET"/>