无法从URL连接中读取数据。清单中允许的权限

时间:2013-09-18 10:38:05

标签: android

我得到了:

  

java.net.SocketException:Permission denied error

在LogCat中

这是我的主要java文件的代码。我还在清单文件中添加了权限INTERNETACCESS_NETWORK_STATE

package com.example.read;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    tv= (TextView)findViewById(R.id.tv1);

    try {
        // Create a URL for the desired page
        URL url = new URL("http://172.16.0.240/test.txt");

        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String all=""; 
        String str;
        while ((str = in.readLine()) != null) {
            all+=str;
        }
        in.close();

        tv.setText(all);


    } catch (MalformedURLException e) {Log.i("asj","mal" );
    } catch (IOException e) {Log.i("asj","io" );
    }
}



} 

0 个答案:

没有答案