1

I am creating SOAP xml parsing ListView.

I am adding (ksoap2-android-assembly-2.6.2-jar-with-dependencies.jar) jar file.

Iam adding permission also ()

I am Using ArrayList.

I am not understanding this error please help me

MainActivity.java

public class MainActivity extends ListActivity  {

// XML node keys
static final String FORMMODEL = "FormModel";
static final String TEXT1 = "Text1";
static final String TEXT2 = "Text2";
static final String TEXT3 = "Text3";


private static final String METHOD_NAME = "HelloWorld";
private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://sygnetinfosol.com/webservice.asmx";
//you can get these values from the wsdl file^

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("bSelected", true);

SoapSerializationEnvelope sse=new SoapSerializationEnvelope(SoapEnvelope.VER11);
sse.setOutputSoapObject(request);
sse.dotNet=true;

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

SoapObject res=(SoapObject) sse.bodyIn;

 final ArrayList<HashMap<String, String>> valuesList = new ArrayList<HashMap<String, String>>();

XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(FORMMODEL);

// looping through all item nodes <item>
for (int i = 0; i < ((SoapObject) nl).getPropertyCount(); i++) {

    SoapObject  namesObject = (SoapObject) res.getProperty(i);
    for(int j=0;j<namesObject.getPropertyCount();j++)
    {
        Object objectNames=namesObject.getProperty(j);
        SoapObject soapObjectText1 = (SoapObject)objectNames;
        SoapObject soapObjectText2 = (SoapObject)objectNames;
        SoapObject soapObjectText3 = (SoapObject)objectNames;


        String sText1 = soapObjectText1.getProperty("iText1").toString();
        String sText2 = soapObjectText2.getProperty("sText2").toString();
        String sText3 = soapObjectText3.getProperty("sText3").toString();

        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);

        // adding each child node to HashMap key => value
        map.put(TEXT1, parser.getValue(e, sText1)); 
        map.put(TEXT2, parser.getValue(e, sText2));
        map.put(TEXT3, parser.getValue(e, sText3));

        System.out.println("MY SOAP RESPONE IS"+ res.getProperty(0).toString());

        // adding HashList to ArrayList
        valuesList.add(map);
    }
}    
// Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, valuesList,R.layout.list_item,
            new String[] { TEXT1, TEXT2, TEXT3 }, new int[] {
                    R.id.lat, R.id.long1, R.id.address });

    setListAdapter(adapter);

    ListView lv = getListView();

    System.out.println("MY SOAP RESPONE IS"+ res.getProperty(0).toString());
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

         HashMap<String, String> map = new HashMap<String, String>();
            map = valuesList.get(position);
        // Starting new intent
        Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
        in.putExtra(TEXT1, map.get(MainActivity.TEXT1));
        in.putExtra(TEXT2, map.get(MainActivity.TEXT2));
        in.putExtra(TEXT3, map.get(MainActivity.TEXT3));

        startActivity(in);

    }
});
}
}

LogCat

03-15 09:26:45.923: E/AndroidRuntime(3371): FATAL EXCEPTION: main
03-15 09:26:45.923: E/AndroidRuntime(3371): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.syg.abc/com.syg.abc.MainActivity}: android.os.NetworkOnMainThreadException
03-15 09:26:45.923: E/AndroidRuntime(3371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at android.os.Looper.loop(Looper.java:137)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at java.lang.reflect.Method.invokeNative(Native Method)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at java.lang.reflect.Method.invoke(Method.java:511)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 03-15 09:26:45.923: E/AndroidRuntime(3371):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 03-15 09:26:45.923: E/AndroidRuntime(3371):    at dalvik.system.NativeStart.main(Native Method)
03-15 09:26:45.923: E/AndroidRuntime(3371): Caused by: android.os.NetworkOnMainThreadException
03-15 09:26:45.923: E/AndroidRuntime(3371):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:76)
 03-15 09:26:45.923: E/AndroidRuntime(3371):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:152)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at com.syg.abc.MainActivity.onCreate(MainActivity.java:56)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at android.app.Activity.performCreate(Activity.java:5104)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-15 09:26:45.923: E/AndroidRuntime(3371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-15 09:26:45.923: E/AndroidRuntime(3371):     ... 11 more
03-15 09:26:46.223: D/dalvikvm(3371): GC_CONCURRENT freed 183K, 12% free 2588K/2912K, paused 110ms+4ms, total 292ms
03-15 09:27:36.943: I/Process(3371): Sending signal. PID: 3371 SIG: 9

mainActivity.java

protected void onPostExecute(Void params) {

ListAdapter adapter = new SimpleAdapter(this, valuesList,R.layout.list_item,
        new String[] { TEXT1, TEXT2, TEXT3 }, new int[] {
                R.id.lat, R.id.long1, R.id.address });

setListAdapter(adapter);

ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

         HashMap<String, String> map = new HashMap<String, String>();
            map = valuesList.get(position);
        // Starting new intent
        Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
        in.putExtra(TEXT1, map.get(MainActivity.TEXT1));
        in.putExtra(TEXT2, map.get(MainActivity.TEXT2));
        in.putExtra(TEXT3, map.get(MainActivity.TEXT3));

        startActivity(in);

  }
 });
}

2 Answers2

1

Your making a network call on your main thread.

NetworkOnMainThreadException is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

http://android-developers.blogspot.in/2009/05/painless-threading.html. I suggest you have a look at the article in the link.

Use a Asynctask to make the soap request.

In your activity

public class MainActivity extends ListActivity  {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                new SoaprequestTask().execute();

       }



private class SoapRequestTask extends AsyncTask<Void, Void, Void> {
 //runs on ui thread.
 protected void onPreExecute() {
     //display progressdialog
 }
   // runs in the background thread. do not update ui from here
 protected Void doInBackground(Void... params) {
        //make a soap request here
 SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);
 request.addProperty("bSelected", true);

 SoapSerializationEnvelope sse=new SoapSerializationEnvelope(SoapEnvelope.VER11);
 sse.setOutputSoapObject(request);
 sse.dotNet=true;

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

   SoapObject res=(SoapObject) sse.bodyIn;

   final ArrayList<HashMap<String, String>> valuesList = new ArrayList<HashMap<String, String>>();

   XMLParser parser = new XMLParser();
   String xml = parser.getXmlFromUrl(URL); // getting XML
   Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(FORMMODEL);

   // looping through all item nodes <item>
   for (int i = 0; i < ((SoapObject) nl).getPropertyCount(); i++) {

    SoapObject  namesObject = (SoapObject) res.getProperty(i);
    for(int j=0;j<namesObject.getPropertyCount();j++)
    {
    Object objectNames=namesObject.getProperty(j);
    SoapObject soapObjectText1 = (SoapObject)objectNames;
    SoapObject soapObjectText2 = (SoapObject)objectNames;
    SoapObject soapObjectText3 = (SoapObject)objectNames;


    String sText1 = soapObjectText1.getProperty("iText1").toString();
    String sText2 = soapObjectText2.getProperty("sText2").toString();
    String sText3 = soapObjectText3.getProperty("sText3").toString();

    // creating new HashMap
    HashMap<String, String> map = new HashMap<String, String>();
    Element e = (Element) nl.item(i);

    // adding each child node to HashMap key => value
    map.put(TEXT1, parser.getValue(e, sText1)); 
    map.put(TEXT2, parser.getValue(e, sText2));
    map.put(TEXT3, parser.getValue(e, sText3));

    System.out.println("MY SOAP RESPONE IS"+ res.getProperty(0).toString());

    // adding HashList to ArrayList
    valuesList.add(map);
    }
     return null;
 }


  //runs on ui thread.update ui here
 protected void onPostExecute(Void params) {
    //dismiss progress dialog and update ui. display data in listview
  }
 }

}

I have just pasted the above code from your question. Make sure you do not make changes to the ui doInBackground(). Display the contents in listview in onPostExecute();

Have a look at the heading The 4 Steps

Alternatively you can use robopsice. https://github.com/octo-online/robospice

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • I am not understanding how to sole that problem please help me – user2021654 Mar 15 '13 at 09:53
  • what your doing is making a soap request on the ui thread. call asynctask after setcontentview() in oncreate(). Make the saop request in doInbackground() – Raghunandan Mar 15 '13 at 09:55
  • soap parsing like SAXParsing exactly where is that problem please help me – user2021654 Mar 15 '13 at 10:02
  • you need to make netowrks calls in the background not on the ui thread. Use a asynctask to make soap request. after getting response you can parser using sax or dom and display the contents in listview. Do not update ui in doInBackground(). Display the contents in onPostExecute() – Raghunandan Mar 15 '13 at 10:10
  • 1
    I am Changed Here error is coming please check protected void onPostExecute(Void params) { ListAdapter adapter = new SimpleAdapter(this, valuesList,R.layout.list_item, new String[] { TEXT1, TEXT2, TEXT3 }, new int[] { R.id.lat, R.id.long1, R.id.address }); – user2021654 Mar 15 '13 at 10:29
  • change this to MainActivity.this.ListAdapter adapter = new SimpleAdapter(MainActiivty.this.....).Pls post what error u r getting – Raghunandan Mar 15 '13 at 10:42
  • Invalid type for the variable onPostExecute please help me – user2021654 Mar 15 '13 at 11:05
0

You are attempting to make a request on your UI thread. The onCreate() method will be called on the UI thread, and performing long operations like this has always been discouraged, and as of Honeycomb (I believe) attempting to do so will now throw an exception.

You probably want to use some resources on Threading in Android from this question.

Community
  • 1
  • 1
wsanville
  • 37,158
  • 8
  • 76
  • 101