Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 4或更高版本上的HTTP连接出错?_Android_Http_Android 4.0 Ice Cream Sandwich_Httpconnection - Fatal编程技术网

Android 4或更高版本上的HTTP连接出错?

Android 4或更高版本上的HTTP连接出错?,android,http,android-4.0-ice-cream-sandwich,httpconnection,Android,Http,Android 4.0 Ice Cream Sandwich,Httpconnection,我正在尝试使用HTTPCLient从JSON文件获取信息。奇怪的是,当我在自己的手机(HTC Desire HD和安卓2.3)或安卓2.1模拟器上运行应用程序时,一切都正常。但当我在装有安卓4.1.2的模拟器、HTC One X(安卓4.1.1)或三星Galaxy SIII(安卓4.0.4)上运行我的应用程序时,应用程序崩溃(请参阅本文末尾的stacktrace) 有人知道问题出在哪里吗?如果没有,还有其他方法调用我的JSONfile吗?也许那时它会起作用 我真的希望有人能帮助我,如果我的问题不

我正在尝试使用
HTTPCLient
从JSON文件获取信息。奇怪的是,当我在自己的手机(HTC Desire HD和安卓2.3)或安卓2.1模拟器上运行应用程序时,一切都正常。但当我在装有安卓4.1.2的模拟器、HTC One X(安卓4.1.1)或三星Galaxy SIII(安卓4.0.4)上运行我的应用程序时,应用程序崩溃(请参阅本文末尾的stacktrace)

有人知道问题出在哪里吗?如果没有,还有其他方法调用我的JSONfile吗?也许那时它会起作用

我真的希望有人能帮助我,如果我的问题不够清楚,请告诉我

RoosterWijzigingen.java(缺少一些代码来缩短它,因为它与问题无关):

清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="pws.sgdbroosterapp"
        android:versionCode="1"
        android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Roosterwijziging"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".RoosterWijzigingen"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".Preferences"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="portrait" >
        </activity>

        <uses-library
              android:name="android.test.runner"
              android:required="true" />
    </application>
    <uses-permission android:name="android.permission.INTERNET" /> 
 </manifest>

使用
separate thread
执行网络请求,因为它会出错。使用
异步
任务执行此操作

您的错误是http连接android.os.NetworkOnMainThreadException


要解决这个问题,请创建一个
异步类
,然后在它的
doinbackground
上对json执行http请求,并在
postexecute
方法中根据需要更新ui。

您正在主线程上运行HttpClient

android.os.NetworkOnMainThreadException
包装您的json调用

JSONObject json = JSONfunctions.getJSONfromURL(URL);
对于异步任务,类似这样的事情

new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            JSONObject json = JSONfunctions.getJSONfromURL(URL);
            Log.v("Stackoverflow", json.toString());
            return null;
        }

    }.execute();
newasynctask(){
@凌驾
受保护的Void doInBackground(Void…参数){
JSONObject json=JSONfunctions.getJSONfromURL(URL);
Log.v(“Stackoverflow”,json.toString());
返回null;
}
}.execute();
不要忘记添加Internet访问权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>


关于

查看此

更改后尝试添加(行+“\n”)添加到
某人(行)因为您的json无效谢谢您的回答!我必须将代码放在你的帖子的OnCreate中,而不是JSONObject json=JSONfunctions.getJSONfromURL(URL);,正确的?但是如果我在代码中的其他地方使用JSONObject json,我会得到一个错误“json无法解析”。也许这是一个愚蠢的问题,但我必须做些什么来修复这个错误呢?我认为您应该阅读AsyncTask文档,但同时您可以尝试:new AsyncTask(){@Override protected String doInBackground(Void…params){//在自己的线程上运行JSONObject json=JSONfunctions.getJSONfromURL(URL);return json.toString();}受保护的void onPostExecute(字符串结果){//在UI线程Log.v(“Stackoverflow”,result);}}}.execute()上运行;我在第一篇文章中编辑了我的RoosterWijzigingen.java代码。我对Android/Java非常陌生,我找不到必须在PostExecute中添加的内容,这样就不会在klassen=json.getJSONArray(“klassen”)中出现错误;也许你可以帮我看看,我真的很感谢你的帮助!不要使用新的文本视图(此);而是使用TextView=(TextView)findViewById(R.id.);view.setText(json.getString(“type”))Android文档中有很多这样的例子。为了从社区获得更多帮助,a也可以帮助其他可能有相同问题的人。你应该接受一个答案。
JSONObject json = JSONfunctions.getJSONfromURL(URL);
new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            JSONObject json = JSONfunctions.getJSONfromURL(URL);
            Log.v("Stackoverflow", json.toString());
            return null;
        }

    }.execute();
<uses-permission android:name="android.permission.INTERNET"></uses-permission>