About Android accessing database through WebService issues

Source: Internet
Author: User
Tags soap

Problem Description:

Access to the database, the phone can delete the database data is not to show the data in the database do not know where the problem, with the HTTP
This is the way I see all the information in my WebService:

 PublicList<string>selectallcargoinfor () {List<string> list =NewList<string>(); Try{String SQL= "SELECT * FROM C"; SqlCommand cmd=NewSqlCommand (Sql,sqlcon); SqlDataReader Reader=cmd.                ExecuteReader ();  while(reader. Read ()) {//Add the result set information to the return vectorList. ADD (reader[0].                    ToString ()); List. ADD (reader[1].                    ToString ()); List. ADD (reader[2].                ToString ()); } reader.                Close (); Cmd.            Dispose (); }            Catch(Exception) {}returnlist; }

Next comes the android:
This is the method of setting the ListView in Mainactivity

 private  void          Setlistview () {listview.setvisibility (view.visible); List  new  arraylist<        hashmap<string, String>> ();        List  = Dbutil.getallinfo (); Adapter  = new  simpleadapter (mainactivity. This   new< /span> string[] {"Cno", "Cname", "Cnum" },  new  int  ["{r.id.txt_cno, R.id.txt_cname,        R.id.txt_cnum});    Listview.setadapter (adapter); }

This is the Operation class:

 PublicListGetallinfo () {ListNewArraylist();        Arraylist.clear ();        Brraylist.clear ();        Crraylist.clear (); NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubCrraylist = Soap.getwebservre ("Selectallcargoinfor", ArrayList, brraylist);                }}). Start (); HashMap<string, string> Temphash =NewHashmap<string, string>(); Temphash.put ("Cno", "Cno"); Temphash.put ("CNAME", "CNAME"); Temphash.put ("Cnum", "Cnum");                List.add (Temphash);  for(intj = 0; J < Crraylist.size (); J + = 3) {HashMap<string, string> HashMap =NewHashmap<string, string>(); Hashmap.put ("Cno", Crraylist.get (j)); Hashmap.put ("Cname", Crraylist.get (j + 1)); Hashmap.put ("Cnum", Crraylist.get (j + 2));        List.add (HASHMAP); }        returnlist; }

Connection WebService that method Httpconnsoap should be no problem because the database of additions and deletions are possible, is unable to achieve this display all the information to the ListView This function do not know why, Logcat is also a green no problem

Information in Logcat:
05-02 15:51:40.642:i/system.out (3678): <?xml version= "1.0" encoding= "Utf-8"? ><soap:envelope xmlns:soap= " http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:xsd=" http ://www.w3.org/2001/XMLSchema "><soap:body><selectallcargoinforresponse xmlns=" http://tempuri.org/" ><selectallcargoinforresult><string>1</string><string>rice</string><string >100</string><string>2</string><string>dog</string><string>50</ String><string>3</string><string> Idiot </string><string>25</string></ Selectallcargoinforresult></selectallcargoinforresponse></soap:body></soap:envelope>
05-02 15:51:40.647:i/system.out (3678): <?xml version= "1.0" encoding= "Utf-8"?
05-02 15:51:40.647:i/system.out (3678): Soap:envelope xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"
05-02 15:51:40.647:i/system.out (3678): Soap:body
05-02 15:51:40.647:i/system.out (3678): Selectallcargoinforresponse xmlns= "http://tempuri.org/"
05-02 15:51:40.647:i/system.out (3678): Selectallcargoinforresult
05-02 15:51:40.647:i/system.out (3678): 0
05-02 15:51:40.647:i/system.out (3678): string>1</string
05-02 15:51:40.647:i/system.out (3678): string>rice</string
05-02 15:51:40.647:i/system.out (3678): string>100</string
05-02 15:51:40.647:i/system.out (3678): string>2</string
05-02 15:51:40.652:i/system.out (3678): string>dog</string
05-02 15:51:40.652:i/system.out (3678): string>50</string
05-02 15:51:40.652:i/system.out (3678): string>3</string
05-02 15:51:40.652:i/system.out (3678): string> idiot </string
05-02 15:51:40.652:i/system.out (3678): string>25</string
05-02 15:51:40.652:i/system.out (3678):/selectallcargoinforresult
05-02 15:51:40.652:i/system.out (3678): 1

Solution 1:

The reason for the analysis is that when the thread has not finished executing, the getallinfo has already been executed, so the crraylist is 0 lines when executing for (int j = 0; J < Crraylist.size (); j + = 3). The logcat you take out is the return data after the request is executed, when the Setlistview method has already gone, so there is only one row of data. A row of data from
hashmap<string, string> temphash = new hashmap<string, string> ();
Temphash.put ("Cno", "Cno");
Temphash.put ("CNAME", "cname");
Temphash.put ("Cnum", "Cnum");
List.add (Temphash);
Use thread should be used with handler.

I'll change the code.

    Private Final Static intrequest_success = 1; Private Final Static intRequest_false = 0; Private voidRequestData () {arraylist.clear ();        Brraylist.clear ();                Crraylist.clear (); NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubCrraylist = Soap.getwebservre ("Selectallcargoinfor", ArrayList, brraylist); Message msg=NewMessage (); if(Crraylist.size () >0) {Msg.what=request_success; }                Else{msg.what=Request_false; }                //Send Messagemhandler.sendmessage (msg);    }}). Start (); }         PublicHandler Mhandler =NewHandler () {//Receiving Messages@Override Public voidhandlemessage (Message msg) {//TODO auto-generated Method Stub              Super. Handlemessage (msg); Switch(msg.what) { CaseRequest_success:setlistview ();  Break;  CaseRequest_false://do error handling                     Break; default:                     Break;              }          }                  }; Private voidSetlistview () {listview.setvisibility (view.visible); ListNewArraylist(); List=Dbutil.getallinfo (); Adapter=NewSimpleadapter (mainactivity. This, List, R.layout.adapter_item,NewString[] {"Cno", "Cname", "Cnum" },           New int[] {r.id.txt_cno, r.id.txt_cname, r.id.txt_cnum});      Listview.setadapter (adapter); }             PublicListGetallinfo () {ListNewArraylist(); HashMap<string, string> Temphash =NewHashmap<string, string>(); Temphash.put ("Cno", "Cno"); Temphash.put ("CNAME", "CNAME"); Temphash.put ("Cnum", "Cnum");                     List.add (Temphash);  for(intj = 0; J < Crraylist.size (); J + = 3) {HashMap<string, string> HashMap =NewHashmap<string, string>(); Hashmap.put ("Cno", Crraylist.get (j)); Hashmap.put ("Cname", Crraylist.get (j + 1)); Hashmap.put ("Cnum", Crraylist.get (j + 2));          List.add (HASHMAP); }          returnlist; }

Execution RequestData, I can not compile, the details of their own adjustment to see, should be able to solve the problem.
You have given too few points, and Daniel has given you no answer. If we solve the problem, we will divide it.

In addition, the Java Multi-threading operation can be studied systematically. Very frequently used in work.

About Android accessing database through WebService issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.