【问题标题】:Error in HTTP connection on Android 4 or higher?Android 4 或更高版本上的 HTTP 连接出错?
【发布时间】:2012-12-19 20:18:19
【问题描述】:

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

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

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

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

package pws.sgdbroosterapp;

public class RoosterWijzigingen extends ListActivity {

    JSONArray jArray;
    String result = null;
    InputStream is = null;
    StringBuilder sb = null;
    JSONArray klassen = null;
    JSONArray mededelingen = null;
    JSONArray rooster = null;
    String status = null;
    String type = null;
    String datum = null;
    String versie = null;
    String minutenperlesuur = null;
    String afwezig = null;
    String mededeling = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState); 

        JSONObject json = JSONfunctions.getJSONfromURL("http://selenight.nl/Wijzigingen/2.1/wijzigingen.json");

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

          //@Override
          //protected String doInBackground(Void... params) {
            // Runs on own thread 
            //JSONObject json = JSONfunctions.getJSONfromURL("http://selenight.nl/Wijzigingen/2.1/wijzigingen.json"); 

            //return json.toString(); 
          //} 

          //protected void onPostExecute(String result) { 
            // someMethod(Send json);
            //Log.v("Stackoverflow", result);
          //}

        //}.execute();


            try{
                // Getting Array of Klassen
                klassen = json.getJSONArray("klassen");

                // Status, type, datum, versie, minutenperlesuur, afwezig en medeleningen worden uit JSOn file gehaald en opgeslagen in Strings
                status = json.getString("status");
                                        //"failure";
                type = json.getString("type");
                datum = json.getString("datum");
                versie = json.getString("versie");
                minutenperlesuur = json.getString("minutenperlesuur");
                afwezig = json.getString("afwezig");
                mededelingen = json.getJSONArray("mededelingen");
                mededeling = mededelingen.toString();


            }catch(JSONException e)        {
                 Log.e("log_tag", "Error parsing data "+e.toString());
            }

        setContentView(R.layout.listplaceholder); 

            if(Integer.parseInt(minutenperlesuur) < 50){
            TextView verkortrooster = new TextView(this);
            verkortrooster.setText("---   Het is "+minutenperlesuur+"min-rooster!  --- ");
            }

            TextView statusdatum = new TextView(this);
            statusdatum.setText("Dit is versie "+versie+" van "+datum+". \n");

            TextView afwezigt = new TextView(this);
            afwezigt.setText("Afwezig:");

            TextView afwezigtextt = new TextView(this);
            afwezigtextt.setText(afwezig+"\n");

            TextView mededelingent = new TextView(this);
            mededelingent.setText("Mededelingen:");

            TextView mededelingentextt = new TextView(this);
            mededelingentextt.setText(mededeling+"\n \n");

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 0, 0, "Settings");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
            case 0:
            startActivity(new Intent(this, Preferences.class));
            return true;
       }
       return false;
    }
 }

JSONfunctions.java:

public class JSONfunctions {
    public static JSONObject getJSONfromURL(String url){
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        //http post
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        } catch(Exception e) { Log.e("log_tag", "Error in http connection" + e.toString()); }


        //convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch(Exception e) { Log.e("log_tag", "Error converting result" + e.toString()); }

        try { jArray = new JSONObject(result); }
        catch(JSONException e) { Log.e("log_tag", "Error parsing data "+e.toString()); }

        return jArray;
    }
 }

清单文件:

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

堆栈跟踪:

01-05 15:12:49.995: E/log_tag(615): Error in http connection android.os.NetworkOnMainThreadException
01-05 15:12:49.995: E/log_tag(615): Error converting result java.lang.NullPointerException
01-05 15:12:49.995: E/log_tag(615): Error parsing data org.json.JSONException: End of input at character 0 of 
01-05 15:12:50.005: D/AndroidRuntime(615): Shutting down VM
01-05 15:12:50.005: W/dalvikvm(615): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-05 15:12:50.015: E/AndroidRuntime(615): FATAL EXCEPTION: main
01-05 15:12:50.015: E/AndroidRuntime(615): java.lang.RuntimeException: Unable to start activity ComponentInfo{pws.sgdbroosterapp/pws.sgdbroosterapp.RoosterWijzigingen}: java.lang.NullPointerException
01-05 15:12:50.015: E/AndroidRuntime(615):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-05 15:12:50.015: E/AndroidRuntime(615):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-05 15:12:50.015: E/AndroidRuntime(615):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-05 15:12:50.015: E/AndroidRuntime(615):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-05 15:12:50.015: E/AndroidRuntime(615):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 15:12:50.015: E/AndroidRuntime(615):  at android.os.Looper.loop(Looper.java:137)
01-05 15:12:50.015: E/AndroidRuntime(615):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-05 15:12:50.015: E/AndroidRuntime(615):  at java.lang.reflect.Method.invokeNative(Native Method)
01-05 15:12:50.015: E/AndroidRuntime(615):  at java.lang.reflect.Method.invoke(Method.java:511)
01-05 15:12:50.015: E/AndroidRuntime(615):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-05 15:12:50.015: E/AndroidRuntime(615):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-05 15:12:50.015: E/AndroidRuntime(615):  at dalvik.system.NativeStart.main(Native Method)
01-05 15:12:50.015: E/AndroidRuntime(615): Caused by: java.lang.NullPointerException
01-05 15:12:50.015: E/AndroidRuntime(615):  at pws.sgdbroosterapp.RoosterWijzigingen.onCreate(RoosterWijzigingen.java:153)
01-05 15:12:50.015: E/AndroidRuntime(615):  at android.app.Activity.performCreate(Activity.java:5008)
01-05 15:12:50.015: E/AndroidRuntime(615):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-05 15:12:50.015: E/AndroidRuntime(615):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)   

【问题讨论】:

  • 在将 sb.append(line + "\n"); 更改为 sb.append(line); 后尝试,因为您的 json 无效

标签: android http android-4.0-ice-cream-sandwich httpconnection


【解决方案1】:

使用separate thread 进行网络请求,因为它会出错。使用asynchronous 任务来完成。

您的错误是 http connection android.os.NetworkOnMainThreadException

要解决它,创建一个asynchronous class,然后在它的doinbackground 上执行 json 的 http 请求,并在 postexecute 方法中根据需要更新您的用户界面。

【讨论】:

    【解决方案2】:

    你正在主线程上运行 HttpClient

    android.os.NetworkOnMainThreadException
    

    包装你的 json 调用

    JSONObject json = JSONfunctions.getJSONfromURL(URL);
    

    使用 AsyncTask,类似这样的

    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>
    

    问候

    【讨论】:

    • 感谢您的回答!我必须将代码放在 OnCreate 而不是 JSONObject json = JSONfunctions.getJSONfromURL(URL); 的帖子中,对吗?但是,如果我在代码中的其他地方使用 JSONObject json,则会收到“无法解析 json”的错误。也许这是一个愚蠢的问题,但我该怎么做才能修复这个错误?
    • 我认为你应该阅读 AsyncTask 文档,但同时你可以尝试: new AsyncTask() { @Override protected String doInBackground(Void... params) { // 运行在自己的线程上 JSONObject json = JSONfunctions.getJSONfromURL(URL);返回 json.toString(); } protected void onPostExecute(String result) { // 在 UI 线程上运行 Log.v("Stackoverflow", result); } }。执行();还可以通过您的新代码获得准确的帮助。
    • 我在第一篇文章中编辑了我的 RoosterWijzigingen.java 代码。我对 Android/Java 很陌生,我找不到我必须放入 onPostExecute 的内容,这样我就不会收到 klassen = json.getJSONArray("klassen"); 的错误。等。也许你可以看看我,非常感谢你的帮助!
    • 不要使用 new TextView(this); 而是使用 TextView view = (TextView) findViewById(R.id.); view.setText(json.getString("type")) Android 文档中有很多这样的例子。
    • 为了从社区中获得更多帮助,也对可能与您有相同问题的其他人有所帮助。你应该接受一个答案。
    【解决方案3】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多