im trying to use the following code in asynce task cause i want to show asynce task to the user for some reasons.
but this asynce task not working at all...
AsyncTask task = new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
String result = "";
InputStream isr = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://persiancoder.tk/appservice/service.php");
//conn.setConnectTimeout(7000);
//YOUR PHP SCRIPT ADDRESS
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
//parse json data
try {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
String n = "";
String a = "";
String j = "";
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
if (i == 0) {
int j1 = json.getInt("id");
if (j1 == 1) {
} else if (j1 == 0) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(Main.this);
dlgAlert.setMessage("برنامه توسط تيم از دسترس خارج شده! لطفا بعدا امتحان کنيد");
dlgAlert.setTitle("Server Out!");
dlgAlert.setCancelable(false);
dlgAlert.setNegativeButton("باشه", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
System.exit(0);
}
});
dlgAlert.show();
}
}
}
} catch (Exception e) {
Log.e("log_tag", "Error Parsing Data " + e.toString());
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(Main.this);
dlgAlert.setMessage("مشکلي نامعلوم پيش آمده!اينترنت خود را چک کنيد");
dlgAlert.setTitle("Unknown Problem!");
dlgAlert.setCancelable(false);
dlgAlert.setNegativeButton("باشه", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
System.exit(0);
}
});
}
return null;
}
};
task.execute(/* optional params */);
i have tried ways but not working.
whats the problem?