我得到了:
在LogCat中java.net.SocketException:Permission denied error
。
这是我的主要java文件的代码。我还在清单文件中添加了权限INTERNET
和ACCESS_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" );
}
}
}