无法使用android连接到sharepoint列表

时间:2014-04-03 11:21:46

标签: android sharepoint

我正在做一个应用程序,用android使用模拟器操纵一个sharePoint列表项。当我运行它时,它会向我显示此消息“很遗憾,您的应用已停止”。 thx帮助我!

这是我的logcat:

    04-03 10:58:37.813: I/dalvikvm(919): threadid=3: reacting to signal 3
04-03 10:58:37.903: I/dalvikvm(919): Wrote stack traces to '/data/anr/traces.txt'
04-03 10:58:38.193: D/AndroidRuntime(919): Shutting down VM
04-03 10:58:38.193: W/dalvikvm(919): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-03 10:58:38.223: E/AndroidRuntime(919): FATAL EXCEPTION: main
04-03 10:58:38.223: E/AndroidRuntime(919): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exemplewebservice/com.exemplewebservice.MainActivity}: android.os.NetworkOnMainThreadException
04-03 10:58:38.223: E/AndroidRuntime(919):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-03 10:58:38.223: E/AndroidRuntime(919):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-03 10:58:38.223: E/AndroidRuntime(919):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-03 10:58:38.223: E/AndroidRuntime(919):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-03 10:58:38.223: E/AndroidRuntime(919):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 10:58:38.223: E/AndroidRuntime(919):  at android.os.Looper.loop(Looper.java:137)
04-03 10:58:38.223: E/AndroidRuntime(919):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-03 10:58:38.223: E/AndroidRuntime(919):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 10:58:38.223: E/AndroidRuntime(919):  at java.lang.reflect.Method.invoke(Method.java:511)
04-03 10:58:38.223: E/AndroidRuntime(919):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-03 10:58:38.223: E/AndroidRuntime(919):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-03 10:58:38.223: E/AndroidRuntime(919):  at dalvik.system.NativeStart.main(Native Method)
04-03 10:58:38.223: E/AndroidRuntime(919): Caused by: android.os.NetworkOnMainThreadException
04-03 10:58:38.223: E/AndroidRuntime(919):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-03 10:58:38.223: E/AndroidRuntime(919):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
04-03 10:58:38.223: E/AndroidRuntime(919):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
04-03 10:58:38.223: E/AndroidRuntime(919):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
04-03 10:58:38.223: E/AndroidRuntime(919):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-03 10:58:38.223: E/AndroidRuntime(919):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-03 10:58:38.223: E/AndroidRuntime(919):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-03 10:58:38.223: E/AndroidRuntime(919):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-03 10:58:38.223: E/AndroidRuntime(919):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-03 10:58:38.223: E/AndroidRuntime(919):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-03 10:58:38.223: E/AndroidRuntime(919):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-03 10:58:38.223: E/AndroidRuntime(919):  at com.exemplewebservice.MainActivity.onCreate(MainActivity.java:40)
04-03 10:58:38.223: E/AndroidRuntime(919):  at android.app.Activity.performCreate(Activity.java:4465)
04-03 10:58:38.223: E/AndroidRuntime(919):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-03 10:58:38.223: E/AndroidRuntime(919):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-03 10:58:38.223: E/AndroidRuntime(919):  ... 11 more
04-03 10:58:38.353: I/dalvikvm(919): threadid=3: reacting to signal 3
04-03 10:58:38.373: I/dalvikvm(919): Wrote stack traces to '/data/anr/traces.txt'
04-03 10:58:38.862: I/dalvikvm(919): threadid=3: reacting to signal 3
04-03 10:58:38.953: I/dalvikvm(919): Wrote stack traces to '/data/anr/traces.txt'

我的主要活动代码如下:

package com.exemplewebservice;

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        tv = (TextView)findViewById(R.id.TextView1);
        tv.setMovementMethod(ScrollingMovementMethod.getInstance());

       try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://*******/_vti_bin/Lists.asmx");


            StringEntity se = new StringEntity( "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><GetListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\"><listName>Extension</listName></GetListItems></soap:Body></soap:Envelope>", HTTP.UTF_8);
            se.setContentType("text/xml");
            httppost.setEntity(se);

            HttpResponse httpresponse = httpclient.execute(httppost);
            HttpEntity resEntity = httpresponse.getEntity();
            tv.setText("Status OK: \n" + EntityUtils.toString(resEntity));

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            tv.setText("Status NOT OK: \n" + e.getMessage());
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            tv.setText("Status NOT OK: \n" + e.getMessage());
        }

    }
}

1 个答案:

答案 0 :(得分:0)

04-03 10:58:38.223: E/AndroidRuntime(919): Caused by: android.os.NetworkOnMainThreadException

您应该在单独的线程(AsyncTask)上运行网络操作:http://developer.android.com/training/articles/perf-anr.html#Avoiding