首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这无故需要android.permission.INTERACT_ACROSS_USERS_FULL。

这无故需要android.permission.INTERACT_ACROSS_USERS_FULL。
EN

Stack Overflow用户
提问于 2014-05-31 19:58:43
回答 1查看 444关注 0票数 0

好吧,我无缘无故地得到了这个错误。因为我的应用程序启动了,一切都很好。但是当它想发送HTTP请求时,它会崩溃,logcat会显示这个错误。

我只在真实的设备上得到这个错误,它在模拟器中运行得很好。

我的应用程序并不需要这样的权限。太简单了。我知道我在代码中做错了什么,这与这样的权限无关。但我不知道我在哪里做错了。

这是我的舱单:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ir.Ava.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="5"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


    <application
        android:allowBackup="false"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Avatheme" 
        android:uiOptions="splitActionBarWhenNarrow" android:supportsRtl="true">
        <!-- Splash screen -->
        <activity
            android:name="ir.Ava.android.SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Main activity -->
        <activity
            android:name="ir.Ava.android.MainActivity"
            android:label="@string/app_name" >
        </activity>
        <activity android:name="ir.Ava.android.Bubble"></activity>
    </application>

</manifest>

这就是导致这个问题的片段:

代码语言:javascript
复制
inside MainActivity.java

public static class profileSectionFragment extends Fragment {

    TextView testText;
    // flag for Internet connection status
    Boolean isInternetPresent = false;
    Button editPFButton;
    // Connection detector class
    ConnectionDetector cd;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.profile, container, false);
        ((TextView) rootView.findViewById(R.id.pf_username)).setText("Name of the user");
        testText = (TextView) rootView.findViewById(R.id.testText);

        // Buttons
        editPFButton = (Button) rootView.findViewById(R.id.edit_profile);

        editPFButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                Context appContext = getActivity().getApplicationContext();

                // creating connection detector class instance
                cd = new ConnectionDetector(appContext);

                // get Internet status
                isInternetPresent = cd.isConnectingToInternet();



                // check for Internet status
                if (isInternetPresent) {
                    testText.setText("successfully connected to the internet");
                } else {
                    testText.setText("there is no internet connection");
                }


                // Creating HTTP client
                HttpClient httpClient = new DefaultHttpClient();
                // Creating HTTP Post
                HttpPost httpPost = new HttpPost(
                        "http://192.168.1.2/android/");

                // Building post parameters
                // key and value pair
                List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
                nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
                nameValuePair.add(new BasicNameValuePair("message",
                        "Hi, trying Android HTTP post!"));

                // Url Encoding the POST parameters
                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
                } catch (UnsupportedEncodingException e) {
                    // writing error to Log
                    e.printStackTrace();
                }

                // Making HTTP Request
                try {
                    HttpResponse response = httpClient.execute(httpPost);
                    Log.d("HTTP Response:", response.toString());

                     if (response.getStatusLine().getStatusCode() == 200)
                     {
                         HttpEntity entity = response.getEntity();
                         testText.setText(EntityUtils.toString(entity));
                     }
                     else
                     {
                         testText.setText("Error: "+response.getStatusLine().getStatusCode());
                     }

                } catch (ClientProtocolException e) {
                    // writing exception to log
                    e.printStackTrace();
                } catch (IOException e) {
                    // writing exception to log
                    e.printStackTrace();

                }

            }
        });


        return rootView;
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-31 20:41:48

它不允许在主(UI)线程上执行网络I/O。您应该使用AsyncTask代替。例如:

代码语言:javascript
复制
final TextView testText = ...

AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>()
{
    @Override
    protected String doInBackground(Void... params)
    {
        ... Create HTTP client, and everything as before ...

        // Making HTTP Request
        HttpResponse response = httpClient.execute(httpPost);
        Log.d("HTTP Response:", response.toString());

        if (response.getStatusLine().getStatusCode() == 200)
        {
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity);
        }
        else
        {
            return "Error: "+response.getStatusLine().getStatusCode();
        }
    }

    @Override
    protected void onPostExecute(String result)
    {
        testText.setText(result);
    }
};

task.execute();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23974151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档