i tried to connect 192.168.1.20/ localhost/ 10.0.2.2 but not not working

i tried to connect to retrofit in Ubuntu. it execute the failure method. i tried port 8080 and 3000 also. i don't know where is problem occur. it perfectly working on server url.

emulator show error is cannot connect to 10.0.2.2:3000

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.brg_dsk005.pikaainstruction">

    <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: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>

function: fetching function from retrofit

private void TaketestReq() {

       /* private JsonObject ApiJsonMap() {
            JsonObject gsonObject = new JsonObject();
            try {
                JSONObject jsonObj_ = new JSONObject();
                jsonObj_.put("key", "value");
                jsonObj_.put("key", "value");
                jsonObj_.put("key", "value");
                JsonParser jsonParser = new JsonParser();
                gsonObject = (JsonObject) jsonParser.parse(jsonObj_.toString());
                //print parameter Log.e("MY gson.JSON: ", "AS PARAMETER " + gsonObject); }
                // catch (JSONException e) { e.printStackTrace(); } return gsonObject; }
*/

        JsonObject gsonObject = new JsonObject();


        JsonObject paramObject = new JsonObject();
        JsonObject current_section = new JsonObject();

        paramObject.addProperty("test_id", 5);
        paramObject.addProperty("user_id", "null");
        paramObject.addProperty("org_id", 2);
        paramObject.addProperty("schedule_id", 15);
        paramObject.addProperty("next_section_id","");
        paramObject.addProperty("group_id", "null");


        paramObject.addProperty("current_section", String.valueOf(current_section));
        JsonParser jsonParser = new JsonParser();
        gsonObject = (JsonObject) jsonParser.parse(paramObject.toString());
        Log.e("MY gson.JSON: ", "AS PARAMETER " + gsonObject);

        Call<Test_Responce> userCall = api_interface.testRes(gsonObject);
        userCall.enqueue(new Callback<Test_Responce>() {
            @Override
            public void onResponse(Call<Test_Responce> call, Response<Test_Responce> response) {
                if (response.isSuccessful()){
                    Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(),response.body().getUserName().toLowerCase(),Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(),"else",Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(Call<Test_Responce> call, Throwable t) {
                Toast.makeText(getApplicationContext(),"fail",Toast.LENGTH_SHORT).show();
            }
        });

    }

retrofit connection :here i attached retrofit service code

public class RetrofitServiceGenrator {
    //private static final String EndPoint = "http://pikatestengine.brigita.co/testengine/public/api/";
    private static final String EndPoint = "http://10.0.2.2:3000/testengine/public/api/";

    private static Api_interface retrofitClient = null;

    public static Api_interface getRetrofitClient(Context context){

        if (retrofitClient == null){

            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

            httpClient.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request();

                    Request request = original.newBuilder()
                            .header("Content-Type", "application/json")
                            .header("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjAyODkyMzI5NzdjNjczZTlmZDExYmVkOGYzNTBiYTY2YjhmMjNjNWVkYjU4ZDM5NmMxM2I3YjJhYjZhOWVkYmMwZTlkZWI0MDhiYmI3MjAzIn0.eyJhdWQiOiIzIiwianRpIjoiMDI4OTIzMjk3N2M2NzNlOWZkMTFiZWQ4ZjM1MGJhNjZiOGYyM2M1ZWRiNThkMzk2YzEzYjdiMmFiNmE5ZWRiYzBlOWRlYjQwOGJiYjcyMDMiLCJpYXQiOjE1MzEyMTk3NjksIm5iZiI6MTUzMTIxOTc2OSwiZXhwIjoxNTYyNzU1NzY5LCJzdWIiOiIyNCIsInNjb3BlcyI6W119.dA6OvGg4rwU13bzVITzOcJpdhUJico1YwaJpAwvcxISghk7yVqEhJunDByAOR1nk5bScwUv5RUERRIbXd1QPk5mxkbmZdZNPGRh__QswWLLaorGB0gma7NwmS42V3zKH0e-n9T6ZJivZP2PgmuTwnTouFv3dE3uf-jc0dK9dYZMw9ClzEY-U7_W75bwUDKOROxFfQf0yIuOR4zPMD0LePgWYheKRI1soYoEfT1hfAjUEMFpjGRlyQy_aVRXYVQJunLyeuaHEYzGhXeNu_Yy1-tiBUqaHDSH6zIYfYXUKtD4-RH33iZcOIEuIh9QPzgf-v-WEPorlroMed9HlLv-ctmcptj9p4kIW8g5jZ6NEuReOKGcjiTFqrikPhiYw7SEhU_Q_lo0gUentJSO_VjQUPsOO0WVAKOGRpgnuDIjs3H2MZTTiae_6cbcLJUGc8_irSTY9i_4AewBQ2e3EQMFdUVf8fFYvYVP3OEA-Ys1bNZiGQCckhLbLroQDgDF6NbnVpUdqvWMMnKignXAtxKAiNcsdRHbfw6DH-Trg5syd0152yXVGxqCx9SpW7d0ypOh98lwwDGr10B7BduRLzkJDrTREP6TwHf34lJ4FAN1LdDvUAjwoaP-d8gmJw6sAl15_in25G6emcMbwXeW0EC-sMY0A6qP5NzKYgYeBsLk8Z9k")
                            .method(original.method(), original.body())
                            .build();

                    return chain.proceed(request);
                }

            });
            OkHttpClient client = httpClient.build();
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(EndPoint)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client).build();

            retrofitClient = retrofit.create(Api_interface.class);
        }

        return retrofitClient;
    }

}

interface:

  @POST("take_tests")
    Call<Test_Responce> testRes (@Body JsonObject paramObject);

1 answer

  • answered 2018-07-11 04:41 Nirav Bhavsar

    Might be possibe, it cant access the localhost network, please add below permission in android manifest and try

    <uses-permission  android:name="android.permission.ACCESS_NETWORK_STATE"/>