java.net.SocketTimeoutException:10000ms后无法连接到/10.0.2.2(端口8080)

时间:2016-12-06 22:41:34

标签: java android rest timeout

我正在尝试将我的Android应用程序与我自己的Rest Service连接。但是我收到了这个错误:

java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (port 8080) after 10000ms

这是我的代码:

public class LoginActivity extends AppCompatActivity implements OnClickListener {

    Button login, exit, back;
    TextView result;
    OkHttpClient client;
    MediaType JSON;


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

        client = new OkHttpClient();
        JSON = MediaType.parse("application/json; charset=utf-8");
    }

    public class GetTask extends AsyncTask {

        private Exception exception;

        @Override
        protected Object doInBackground(Object[] params) {
            try {
                String getResponse = get("http://10.0.2.2:8080/ServicioRestTFG/rest/UsuariosServicesRs/");
                return getResponse;
            } catch (Exception e) {
                this.exception = e;
                return null;
            }
        }

        public String get(String url) throws IOException {
            Request request = new Request.Builder().url(url).build();
            Response response = client.newCall(request).execute();
            return response.body().string();
        }
    }

    @Override
    public void onClick(View v) {
        GetTask task = new GetTask();
        task.execute();
    }

我不知道是什么问题。我读到一个解决方案是增加超时,但这不是我的解决方案,因为我尝试它,它不适合我。

0 个答案:

没有答案