kitkat连接

7kjnsjlb  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(344)

我是java和android编程的新手,但我接受了一个朋友的挑战,现在我必须努力工作。
我终于设法让这种类型的活动与asynctask一起工作,但它似乎在所有android上都能正常工作,但在4.4.2kitkat上却不行。
这个问题似乎是在url.openconnection,我尝试了很多次,以改变我这样做的方式,但我没有得到积极的结果。。。我只需从url读取文件这是我的类代码:

public class MenuActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        new HttpTask().execute();
    }

    public final class HttpTask
            extends
            AsyncTask<String/* Param */, Boolean /* Progress */, String /* Result */> {

        private HttpClient mHc = new DefaultHttpClient();

        @Override
        protected String doInBackground(String... params) {
            publishProgress(true);
            InputStream inputstream = null;

            URL url = null;
            try {
                url = new URL("http://somesite/prova.txt");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            assert url != null;
            URLConnection connection = null;
            try {
                connection = url.openConnection();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                HttpURLConnection httpConnection = (HttpURLConnection) connection;
                httpConnection.setRequestMethod("GET");
                httpConnection.connect();

                if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    inputstream = httpConnection.getInputStream();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            ByteArrayOutputStream bytearryoutputstream = new ByteArrayOutputStream();
            int i;
            try {
                i = inputstream.read();
                while (i != -1) {
                    bytearryoutputstream.write(i);
                    i = inputstream.read();
                }
                inputstream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bytearryoutputstream.toString();
        }

        @Override
        protected void onProgressUpdate(Boolean... progress) {

        }

        @Override
        protected void onPostExecute(String result) {

            StringBuilder nuovafrase=new StringBuilder("");

            String[] frasone=result.split("\n");

            ListView listView = (ListView)findViewById(R.id.listViewDemo);

            ArrayAdapter<String> arrayAdapter;
            arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.rowmenu, R.id.textViewList, frasone);
            listView.setAdapter(arrayAdapter);

        }

    }
}

这是logcat。。。

03-11 17:49:37.955    1277-1294/com.example.appsb.app W/System.err﹕                 atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
      03-11 17:49:37.955    1277-1294/com.example.appsb.app W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
       03-11 17:49:37.959    1277-1294/com.example.appsb.app W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
       03-11 17:49:37.959    1277-1294/com.example.appsb.app W/System.err﹕ Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
         03-11 17:49:37.959    1277-1294/com.example.appsb.app W/System.err﹕ at libcore.io.Posix.getaddrinfo(Native Method)
       03-11 17:49:37.959    1277-1294/com.example.appsb.app W/System.err﹕ at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
         03-11 17:49:37.959    1277-1294/com.example.appsb.app W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
      03-11 17:49:37.959    1277-1294/com.example.appsb.app W/System.err﹕ ... 18 more
        03-11 17:49:37.959    1277-1294/com.example.appsb.app W/System.err﹕ Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
        03-11 17:49:37.959    1277-1294/com.example.appsb.app W/System.err﹕ ... 21 more
         03-11 17:49:37.963    1277-1294/com.example.appsb.app W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0xa4d69b20)
             03-11 17:49:37.963    1277-1294/com.example.appsb.app E/AndroidRuntime﹕               FATAL EXCEPTION: AsyncTask #1
         Process: com.example.appsb.app, PID: 1277
             java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at               java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                   at java.lang.Thread.run(Thread.java:841)
             Caused by: java.lang.NullPointerException
                   at          com.example.appsb.app.MenuActivity$HttpTask.doInBackground(MenuActivity.java:74)
                    at com.example.appsb.app.MenuActivity$HttpTask.doInBackground(MenuActivity.java:33)
                   at android.os.AsyncTask$2.call(AsyncTask.java:288)
                    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                      at java.lang.Thread.run(Thread.java:841)

有一个原因是: java.lang.NullPointerException 但我不明白为什么。。。
谢谢

pzfprimi

pzfprimi1#

可能是因为 inputstream 存在 null . inputstream 仅当响应代码为 OK . 所以您需要检查返回的响应代码。如果它没有导致错误,我仍然会添加一些代码,如果响应代码不是 OK . 如果你的应用程序无法连接,你不希望它崩溃。您至少应该显示一个有用的错误消息示例:

else{
    showErrorMsg();
    return null;
}
wbrvyc0a

wbrvyc0a2#

尝试inputstream.read()时捕获filenotfoundexception;

try {
    i = inputstream.read();
    while (i != -1) {
        bytearryoutputstream.write(i);
        i = inputstream.read();
    }
    inputstream.close();
} catch (FileNotFoundException e) {
    Log.e("MyTag","Handling empty page...");
} catch (IOException e) {
    Log.e("MyTag",e.toString());
}

相关问题