PDA

View Full Version : مشکل این کلاس و activity



keyhan.taktaz
شنبه 21 دی 1392, 12:37 عصر
سلام
من یه activity دارم.
موقعی که کاربر رو دکمه کلیک میکنه یک شی از یک کلاس AsyncTask میسازم.اما اصلا کار نمیکنه.
کلاس بدون AsyncTask کار میکرد.اما وقتی AsyncTask رو اضافه کردم تا بتونم Waiting بذارم براش دیگه کار نکرد
package com.z;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;


public class Connection extends AsyncTask<JSONObject, Void, Boolean>{
private static final String TAG = "MyActivity";
private String temp = null;
protected static String url = LinkManager.getUrl();
private ProgressDialog dialog;

public Connection(Activity activity){
dialog = new ProgressDialog(activity);
dialog.setTitle("zzzzz");
dialog.setMessage("aaaaa");
if(!dialog.isShowing()){
dialog.show();
}
}
public static String POST(JSONObject data){

InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = "";
json = data.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
{
result = convertInputStreamToString(inputStream);
Log.v(TAG, result);
}
else
{
result = "Did not work!";
}

} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}

private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;

inputStream.close();
return result;

}
@Override
protected void onPreExecute() {
// initialize the progress bar
// set maximum progress to 100.
Log.i(TAG, "startsssssssssss");
dialog.show();

}


@Override
protected Boolean doInBackground(JSONObject... params) {

boolean status = false;
temp = POST(params[0]);
if(temp != null)
{
status = true;
}
Log.i(TAG, "errrrrrrrrrrrrrrrrrrrrr");
return status;

}

@Override
protected void onPostExecute(Boolean result) {
// async task finished
dialog.dismiss();

}

}

اینم کد مربوط به ایجاد شی در قسمت activity :
connection = new Connection(activity);
connection.execute(json);

rubiks.kde
شنبه 21 دی 1392, 13:01 عصر
خطا داره؟

خروجی logcat رو بزارید.

keyhan.taktaz
شنبه 21 دی 1392, 13:06 عصر
قسمت constract اجرا میشه.اما اصلا وارد قسمت doInBackground نمیشه.

keyhan.taktaz
شنبه 21 دی 1392, 13:33 عصر
rubiks.kde (http://barnamenevis.org/member.php?231180-rubiks.kde) جان مشکل رو پیدا کردم.مشکل از کد های dialog هست که تو کلاس نوشتم.همشو پاک کردم اجرا شد.
پس الان چجور دیالوگ نمایش بدم :'(

keyhan.taktaz
شنبه 21 دی 1392, 13:45 عصر
ممنون دوست عزیز مشکل رو حل کردم.