SORU
25 EYLÜL 2012, Salı


OnPostExecute sonucu elde etmek için nasıl() AsyncTask ayrı bir sınıf olduğu için ana faaliyet için?

Bu iki Dersim var. Benim ana Etkinlik ve AsyncTask uzanan bir, Şimdi benim ana Faaliyeti AsyncTask içinde OnPostExecute sonuç almak istiyorum. Nasıl ya benim asıl Faaliyet sonucu pass alabilir miyim?

Burada örnek kodlar.

Benim ana Etkinlik.

public class MainActivity extends Activity{

    AasyncTask asyncTask = new AasyncTask();

    @Override
    public void onCreate(Bundle aBundle) {
        super.onCreate(aBundle);            

        //Calling the AsyncTask class to start to execute.  
        asyncTask.execute(a.targetServer); 

        //Creating a TextView.
        TextView displayUI = asyncTask.dataDisplay;
        displayUI = new TextView(this);
        this.setContentView(tTextView); 
    }

}

Bu AsyncTask sınıfı

public class AasyncTask extends AsyncTask<String, Void, String> {

TextView dataDisplay; //store the data  
String soapAction = "http://sample.com"; //SOAPAction header line. 
String targetServer = "https://sampletargeturl.com"; //Target Server.

//SOAP Request.
String soapRequest = "<sample XML request>";    



@Override
protected String doInBackground(String... string) {

String responseStorage = null; //storage of the response

try {


    //Uses URL and HttpURLConnection for server connection. 
    URL targetURL = new URL(targetServer);
    HttpURLConnection httpCon = (HttpURLConnection) targetURL.openConnection();
    httpCon.setDoOutput(true);
    httpCon.setDoInput(true);
    httpCon.setUseCaches(false); 
    httpCon.setChunkedStreamingMode(0);

    //properties of SOAPAction header
    httpCon.addRequestProperty("SOAPAction", soapAction);
    httpCon.addRequestProperty("Content-Type", "text/xml; charset=utf-8"); 
    httpCon.addRequestProperty("Content-Length", ""   soapRequest.length());
    httpCon.setRequestMethod(HttpPost.METHOD_NAME);


    //sending request to the server.
    OutputStream outputStream = httpCon.getOutputStream(); 
    Writer writer = new OutputStreamWriter(outputStream);
    writer.write(soapRequest);
    writer.flush();
    writer.close();


    //getting the response from the server
    InputStream inputStream = httpCon.getInputStream(); 
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(50);

    int intResponse = httpCon.getResponseCode();

    while ((intResponse = bufferedReader.read()) != -1) {
        byteArrayBuffer.append(intResponse);
    }

    responseStorage = new String(byteArrayBuffer.toByteArray()); 

    } catch (Exception aException) {
    responseStorage = aException.getMessage(); 
    }
    return responseStorage;
}

protected void onPostExecute(String result) {

    aTextView.setText(result);

}       

}   

CEVAP
25 EYLÜL 2012, Salı


kolay. interface Sınıf oluşturun.

public interface AsyncResponse {
    void processFinish(String output);
}

zaman Uyumsuz sınıfında, beyan etmek gerekir (arayüz : AsyncResponse):

public class MyAsyncTask extends AsyncTask{
public AsyncResponse delegate=null;

   @Override
   protected void onPostExecute(String result) {
      delegate.processFinish(result);
   }

son adım: 10 ** gerek ana Faaliyet arayüzü önceki AsyncResponse. oluşturduğunuz

public class MainActivity implements AsyncResponse{
   MyAsyncTask asyncTask =new MyAsyncTask();
    @Override
    public void onCreate(Bundle savedInstanceState) {
     asyncTask.delegate = this;
    }


   void processFinish(String output){
     //this you will received result fired from async class of onPostExecute(result) method.
   }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • AceHoodVEVO

    AceHoodVEVO

    12 Mayıs 2009
  • GoProTutorials

    GoProTutoria

    18 NİSAN 2011
  • MkElite

    MkElite

    13 NİSAN 2012