Ahmet Özdemir
Ahmet Özdemir

Reputation: 69

how to call asynctask

I have a web service and this web service return Json string.I want webservice result insert into adapter. But i dont call asynctask class. I've been working on this for 4-5 days and couldn't get it work. Please show me a way to do this...

Note:my code:

package com.kalem.teaapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class BakiyeListesi extends Activity {
TextView textmusteriAdi, textBakiye;
ListView listBakiye;
Button backtomusteriAnaSayfasi;
DatabaseTeaApp v3;
List<BakiyeListesiAlanlar> bakiyeler = new ArrayList<BakiyeListesiAlanlar>();

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bakiyeekrani);

textmusteriAdi = (TextView) findViewById(R.id.textMusteriAdi);
textBakiye = (TextView) findViewById(R.id.textBakiyeMarka);
listBakiye = (ListView) findViewById(R.id.listBakiye);
backtomusteriAnaSayfasi = (Button) findViewById(R.id.btnBackToMusteriAnaSayfasi);
v3 = new DatabaseTeaApp(this);

AsyncCallWS callWS = new AsyncCallWS();
callWS.execute();

final BakiyeListesiAdapter adaptorumuz = new BakiyeListesiAdapter(this,
bakiyeler);
listBakiye.setAdapter(adaptorumuz);

backtomusteriAnaSayfasi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
backtomusteriAnaSayfasi.setTextColor(Color.YELLOW);
Intent intent = new Intent(BakiyeListesi.this,
MusteriAnaSayfasi.class);
startActivity(intent);
finish();

}
});

}

public String getproductList(int CustomerID) {
// depo kodu geldi.
String NAMESPACE = "http://webservice.Kalemyazilim.com";
String URL = "http://212.146.135.169:8081/Android.asmx";
SoapObject request = new SoapObject(NAMESPACE, "CustActivityList");
request.addProperty("CustomerID", CustomerID);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);

envelope.dotNet = true;// web servisimizin .net te yazıldığını
// belirtiyoruz
envelope.setOutputSoapObject(request);

HttpTransportSE senderEnvelope = new HttpTransportSE(URL);

SoapPrimitive result = null;
// bunlaar servisteki ikinci tabloda bulunur.
// soapobject <xsd:schema>schema</xsd:schema>xml</wsTesellumResult>

// SoapPrimitive integer string değerler döndüreceğimiz zaman
// soapAbjectin

try {
// olmazsa while kullan
senderEnvelope.call(
"http://webservice.Kalemyazilim.com/CustActivityList",
envelope);
result = (SoapPrimitive) envelope.getResponse();

} catch (IOException e) {
System.out.println("hatamız:" + e.getMessage());
// TODO Auto-generated catch block
return "-1";
} catch (XmlPullParserException e) {
e.printStackTrace();
// TODO Auto-generated catch block
return "-2";
}
return result.toString();
}

private class AsyncCallWS extends AsyncTask<String, JSONStringer, Void> {

JSONArray jArray;
ProgressDialog pDialog;

@Override
protected Void doInBackground(String... params) {
// takip no:99915984 takipseri: 0000
try {
jArray = new JSONArray(
getproductList(Integer.parseInt(readID(v3))));
} catch (Exception e) {
// TODO: handle exception
}

return null;
}

@Override
protected void onPostExecute(Void result) {

try {

for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);

try {
bakiyeler.add(new BakiyeListesiAlanlar(json.getString("ProdName"),Integer.toString(json.getInt("Amount")), Integer.toString(json.getInt("Brand"))));




} catch (Exception e) {
e.printStackTrace();
}

}
// pDialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}

}

@Override
protected void onPreExecute() {
// pDialog = new ProgressDialog(BakiyeListesi.this);
// pDialog.setMessage("Yükleniyor...");
// pDialog.show();
}
}

public String readID(DatabaseTeaApp v1) {
String id = "";
String[] stunlar = { "Id" };
int i = 0;
SQLiteDatabase db = v1.getReadableDatabase();

Cursor okunanlar = db.query("LoginUser", stunlar, null, null, null,
null, null);
while (okunanlar.moveToLast() && i == 0) {
id = okunanlar.getString(okunanlar.getColumnIndex("Id"));
i++;
}

return id;
}

}

package com.kalem.teaapp;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class BakiyeListesiAdapter extends BaseAdapter {
private LayoutInflater mInflaterList;
private List<BakiyeListesiAlanlar> BakiyeList;

Activity activityBakiyeList;

BakiyeListesiAdapter(Activity activity, List<BakiyeListesiAlanlar> bakiye) {
// XML'i alıp View'a çevirecek inflater'ı örnekleyelim
mInflaterList = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// gösterilecek listeyi de alalım
BakiyeList = bakiye;
activityBakiyeList = activity;// database için
}

@Override
public int getCount() {
return BakiyeList.size();
}

@Override
public BakiyeListesiAlanlar getItem(int position) {
// şöyle de olabilir: public Object getItem(int position)
return BakiyeList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View satirView;

// v1=new Database(tAc); database için oluşturulan conscroctur.
satirView = mInflaterList.inflate(R.layout.bakiyeekranialanlar, null);
TextView textBakiyeMarka = (TextView) satirView
.findViewById(R.id.textBakiyeMarka);
TextView textBakiyeMiktar = (TextView) satirView
.findViewById(R.id.textBakiyeMiktar);
TextView textBakiyeUrun = (TextView) satirView
.findViewById(R.id.textBakiyeUrun);

BakiyeListesiAlanlar bakiye = BakiyeList.get(position);

textBakiyeMarka.setText(bakiye.getMarka());
textBakiyeMiktar.setText(bakiye.getMiktar());
textBakiyeUrun.setText(bakiye.getUrun());

if (position % 2 == 0 || position == 0)
satirView.setBackgroundColor(Color.parseColor("#0057a4"));

return satirView;
}

}

package com.kalem.teaapp;

public class BakiyeListesiAlanlar {
String urun,miktar,marka;

public BakiyeListesiAlanlar(String urun, String miktar, String marka) {
super();
this.urun = urun;
this.miktar = miktar;
this.marka = marka;
}

public String getUrun() {
return urun;
}

public void setUrun(String urun) {
this.urun = urun;
}

public String getMiktar() {
return miktar;
}

public void setMiktar(String miktar) {
this.miktar = miktar;
}

public String getMarka() {
return marka;
}

public void setMarka(String marka) {
this.marka = marka;
}


}

Upvotes: 0

Views: 90

Answers (1)

J.Vassallo
J.Vassallo

Reputation: 2292

You have the getView in the base adapter not optimized, use Holders which you set and get tag objects depending on if the convertView is null.

apart you can run an async task by creating a new instance of it.

new AsyncCallWS().execute("");

Upvotes: 1

Related Questions