1

ListView を解析する SOAP xml を作成しています。

(ksoap2-android-assembly-2.6.2-jar-with-dependencies.jar) jar ファイルを追加しています。

私も許可を追加しています()

私はArrayListを使用しています。

このエラーがわかりません 助けてください

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);

  }
 });
}
4

2 に答える 2

1

メインスレッドでネットワーク呼び出しを行う。

NetworkOnMainThreadExceptionは、アプリケーションがメインスレッドでネットワーク操作を実行しようとするとスローされます。

これは、HoneycombSDK以降を対象とするアプリケーションに対してのみスローされます。以前のSDKバージョンを対象とするアプリケーションは、メインのイベントループスレッドでネットワークを構築できますが、お勧めできません。

http://android-developers.blogspot.in/2009/05/painless-threading.html。リンクの記事をご覧になることをお勧めします。

Asynctaskを使用してsoapリクエストを作成します。

あなたの活動で

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
  }
 }

}

あなたの質問から上記のコードを貼り付けました。ui doInBackground()に変更を加えないように注意してください。onPostExecute();のリストビューにコンテンツを表示します。

見出し「4つのステップ」をご覧ください

または、robopsiceを使用することもできますhttps://github.com/octo-online/robospice

于 2013-03-15T09:37:51.593 に答える
0

UIスレッドでリクエストを行おうとしています。このonCreate()メソッドはUIスレッドで呼び出され、このような長い操作を実行することは常に推奨されていません。Honeycomb(私は信じています)の時点で、そうしようとすると例外がスローされます。

この質問から、Androidのスレッドに関するいくつかのリソースを使用することをお勧めします。

于 2013-03-15T09:38:33.110 に答える