Soru & Cevap

Android ile .NET Web Servisten List Tipindeki Veriyi Alma

18.12.2014 - 03:56

Merhaba arkadaşlar ;

Android ile Web Servisten dönen list tipindeki veriyi alamıyorum .Web Servis sorunsuz çalışıyor. 

[code] 

public class ProductBusiness
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public byte[] Image { get; set; }
    }

 public class ProductBusinessService : System.Web.Services.WebService
    {

        HealthyFoodEntities db;

        public ProductBusinessService()
        {
            db = new HealthyFoodEntities();
        }

        [WebMethod]
        public List<ProductBusiness> getProduct()
        {
            var query =   from x in db.Products
                          select new ProductBusiness
                            {
                                Id= x.Id,
                                Image = x.Picture,
                                Name = x.ProductName
                                
                            };
                
                return query.ToList();  
            
        }

[/code] 

Bu kodlar .NET tarafında veritabanından veri çekmek için yazdığım kodlar ve sorunsuz çalışıyor.Android tarafında da şu şekilde verilere erişmeye çalışıyorum.

[code]

public class Product {

    private int id;
    private String name;
    private byte[] image;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public byte[] getImage() {
        return image;
    }
    public void setImage(byte[] image) {
        this.image = image;
    }

public class Main extends ActionBarActivity {

    List<Product> myList;
    TextView textView;
    Button button;

public static final String NAMESPACE="http://tempuri.org/";                 
    public static final String SOAP_ACTION="http://tempuri.org/getProduct";
    public static final String URL="http://192.168.1.31/HealthyNutrition/ProductBusinessService.asmx";
    public static final String METHOD_NAME="getProduct";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textView = (TextView) findViewById(R.id.textView1);
        button = (Button) findViewById(R.id.button1);
        myList = new ArrayList<Product>();

 

public void onClick(View v)
    {
        Thread nt=new Thread(){ 

            
            public void run(){    
            
            SoapObject request=new SoapObject(NAMESPACE,METHOD_NAME);    

            SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet=true; 
            
            envelope.setOutputSoapObject(request);
            
            HttpTransportSE transport=new HttpTransportSE(URL);
            
            try {
            transport.call(SOAP_ACTION, envelope);
            SoapObject response=(SoapObject) envelope.getResponse(); 
            final int intPropertyCount = response.getPropertyCount();
            
        /*    for (int i = 0; i < intPropertyCount; i++) {
                 SoapObject responseChild = (SoapObject) response.getProperty(i);
                 Product product = new Product();
                 
                 if(responseChild.hasProperty("Id")){
                        product.setId(responseChild.getPropertyAsString("Id"));
                    }
                 if(responseChild.hasProperty("Name")){
                        product.setName(responseChild.getPropertyAsString("Name"));
                    }
                 if(responseChild.hasProperty("Image")){
                        product.setImage(responseChild.getPropertyAsString("Image"));
                    }
                 myList.add(product);
                 }*/
            textView.setText(intPropertyCount);
            
            } catch (HttpResponseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            }
            };
            nt.start(); 
        }

    }
}

[/code]

Property count tu bile alamıyorum.Uygulama durduruluyor.Nasıl yapabilirim?

17 Görüntülenme

2 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user esmeakdag
esmeakdag
11.11.2016 - 01:55

public class MainActivity extends AppCompatActivity {
    public final static String URL = "http://web.gunyaz.com/Service.asmx?WSDL";
    public static final String NAMESPACE = "http://tempuri.org";
    public static final String SOAP_ACTION = "http://tempuri.org/Raporlar_List";
    private static final String METHOD = "Raporlar_List";
    public ArrayList<String> list;
    private TextView textView;
    public ListView listview;
  public   ArrayAdapter adapter;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView2);

        AsyncTaskRunner runner = new AsyncTaskRunner();
        runner.execute();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Main Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }

    private class AsyncTaskRunner extends AsyncTask<Object, Object, ArrayList<String>> {


        protected ArrayList<String> doInBackground(Object... params) {
            publishProgress("Loading contents..."); // Calls onProgressUpdate()

            try {

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                SoapObject request = new SoapObject(NAMESPACE, METHOD);
                request.addProperty("param[0]", "Satis_Raporu");
                request.addProperty("param2[0]", "WIN_P1DRPBTL2D2");
                request.addProperty("param2[1]", "UsiS_V65");
                envelope.dotNet = true;
                envelope.bodyOut = request;


                HttpTransportSE transport = new HttpTransportSE(URL);
                try {
                    transport.call(SOAP_ACTION, envelope);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }

                if (envelope.bodyIn != null) {
                    SoapObject response = (SoapObject) envelope.bodyIn;


                    ArrayList<String> arrayList = new ArrayList<String>();

                    int intPropertyCount = response.getPropertyCount();


                    for (int i = 0; i < intPropertyCount; i++) {
                        arrayList.add(response.getProperty(i).toString());


                    }


                }


            } catch (Exception e) {
                e.printStackTrace();
                //resp = e.getMessage();
            }


            return list;
        }


        protected void onPostExecute(ArrayList<String> result) {
            // execution of result of Long time consuming operation
            // In this example it is the return value from the web service
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // Set adapter
                    adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.activity_main, R.id.product_name, list);

                    listview.setAdapter(adapter);
                }
            });

        }


        protected void onProgressUpdate(Object... text) {

            // Things to be done while execution of long running operation is in
            // progress. For example updating ProgessDialog
        }


    }

}

Arkadaşlar asp.net web servisten list tipinde dönen veriyi listview' e atmak istiyorum fakat program direk durduruluyor. Sizce neden?

 

Profile picture for user yunus.sntrk
yunus.sntrk
13.03.2015 - 10:14

Bu sorunu çözebildiniz mi. Çünkü aynı sorundan ben de muzdaribim.

Cihad
13.03.2015 - 03:05
Çözdüm kardeş örnek koda bakabilirsin.İyi Çalışmalar public List getComments(){ SoapObject request=new SoapObject(NAMESPACE,METHOD_NAME); SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet=true; envelope.setOutputSoapObject(request); HttpTransportSE transport=new HttpTransportSE(URL); try { transport.call(SOAP_ACTION, envelope); SoapObject result=(SoapObject) envelope.bodyIn; SoapObject array = (SoapObject) result.getProperty(0); final int intPropertyCount = array.getPropertyCount(); for (int i = 0; i < intPropertyCount; i++) { SoapObject responseChild = (SoapObject) array.getProperty(i); NewsFeedData data = new NewsFeedData(); if(responseChild.hasProperty("UserName")){ data.setUserName(responseChild.getPropertyAsString("UserName")); } if(responseChild.hasProperty("UserPicture")){ data.setUserPicture(stringToBitmap(responseChild.getPropertyAsString("UserPicture"))); } if(responseChild.hasProperty("ProductPicture")){ data.setProductPicture(stringToBitmap(responseChild.getPropertyAsString("ProductPicture"))); } if(responseChild.hasProperty("Description")){ data.setDescription(responseChild.getPropertyAsString("Description")); } if(responseChild.hasProperty("BarcodeNo")){ data.setBarcodeNo(responseChild.getPropertyAsString("BarcodeNo")); } list.add(data); } } catch (HttpResponseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }