如何允许应用程序从Internet提取数据

时间:2019-11-03 06:06:52

标签: android

我尝试使用android studio中的改造从Internet上获取数据,但出现错误消息   java.net.SocketException:套接字失败:EPERM(不允许操作) 这是我的androidmanifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fetch">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我在MainActivity中的代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Api api = retrofit.create(Api.class);

        Call<List<Hero>> call=api.getHeroes();
        call.enqueue(new Callback<List<Hero>>() {
            @Override
            public void onResponse(Call<List<Hero>> call, Response<List<Hero>> response) {
                List<Hero> heroes = response.body();
                for(Hero h: heroes){
                    Log.d("name", h.getName());

                }
            }

            @Override
            public void onFailure(Call<List<Hero>> call, Throwable t) {
                Log.d("error",t.toString());
                Toast.makeText(getApplicationContext(),
                        t.getMessage(),Toast.LENGTH_SHORT);
            }
        });

    }
}

这是我在Api.java中的代码

public interface Api {
    String base_url="http://simplifiedcoding.net/demos/marvel/";
    @GET("marvel")
    Call<List<Hero>> getHeroes();

}

0 个答案:

没有答案