1

I have two questions:

  • Why am I getting null for the URL?

  • Am I connecting my localhost to my Android Studio emulator correctly?

If not then how can I connect my Android Studio to my local host?

MainActivity.java

import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;

public class MainActivity extends AppCompatActivity {

    String url = "http://10.0.2.2:8000/";
    TextView  text_name_1, text_city_1 ;

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


        text_name_1 = (TextView) findViewById(R.id.text_name_1);
        text_name_1 = (TextView) findViewById(R.id.text_city_1);
        Button ButtonArray= (Button) findViewById(R.id.RetrofitArray);
        ButtonArray.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View VisibleArray = findViewById(R.id.RetrofitArray);
                VisibleArray.setVisibility(View.GONE);
                getRetrofitArray();
            }
        });

    }


    void getRetrofitArray() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class);

        Call<List<Healthinfo>> call = service.getHealthDetails();

        call.enqueue(new Callback<List<Healthinfo>>() {
            @Override
            public void onResponse(Response<List<Healthinfo>> response, Retrofit retrofit) {

                try {

                    List<Healthinfo> StudentData = response.body();

                    for (int i = 0; i < StudentData.size(); i++) {

                        if (i == 0) {
                            text_name_1.setText("UserName  :  " + StudentData.get(i). getUsername());
                            text_city_1.setText("city  : " + StudentData.get(i).getCity());
                        }
                    }


                } catch (Exception e) {
                    Log.d("onResponse", "There is an error");
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Throwable t) {
                Log.d("onFailure", t.toString());
            }
        });
    }
}

RetorfitArrayAPI

import retrofit.http.GET;
public interface RetrofitArrayAPI {

    /*
     * Retrofit get annotation with our URL
     * And our method that will return us details of student.
    */

    @GET("/try/salma/123")
    Call<List<Healthinfo>> getHealthDetails();

}

I have already checked my REST API in postman and I am getting the following output:

[{"user_id":"8","username":"salma","email":"salma@hotmail.com","birthday":"2017-01-12","gender":"","skills":"","country":"Pak","city":"Peshawar","number":"8967","password":"123","cpassword":"123","img":""}]

I am getting the following error in my Android emulator:

01-20 18:28:34.419 2762-2762/com.example.fahadpirzada.motherhealth I/art: Not late-enabling -Xcheck:jni (already on)
01-20 18:28:34.421 2762-2762/com.example.fahadpirzada.motherhealth I/art: Late-enabling JIT
01-20 18:28:34.445 2762-2762/com.example.fahadpirzada.motherhealth I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
01-20 18:28:34.922 2762-2762/com.example.fahadpirzada.motherhealth W/System: ClassLoader referenced unknown path: /data/app/com.example.fahadpirzada.motherhealth-2/lib/x86
01-20 18:28:35.843 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 836.103ms
01-20 18:28:35.984 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 49.509ms
01-20 18:28:36.340 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 49.543ms
01-20 18:28:36.941 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 234.781ms
01-20 18:28:37.263 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 69.193ms
01-20 18:28:38.013 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 21.492ms
01-20 18:28:38.410 2762-2828/com.example.fahadpirzada.motherhealth D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-20 18:28:38.470 2762-2762/com.example.fahadpirzada.motherhealth D/: HostConnection::get() New Host Connection established 0xa44ab260, tid 2762
01-20 18:28:38.593 2762-2828/com.example.fahadpirzada.motherhealth I/OpenGLRenderer: Initialized EGL, version 1.4
01-20 18:28:38.669 2762-2828/com.example.fahadpirzada.motherhealth W/EGL_emulation: eglSurfaceAttrib not implemented
01-20 18:28:38.669 2762-2828/com.example.fahadpirzada.motherhealth W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xadadf2a0, error=EGL_SUCCESS
01-20 18:28:38.793 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 38.637ms
01-20 18:28:39.266 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 13.926ms
01-20 18:28:42.839 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 58.303ms
01-20 18:28:42.845 2762-2762/com.example.fahadpirzada.motherhealth I/Choreographer: Skipped 36 frames!  The application may be doing too much work on its main thread.
01-20 18:28:44.157 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 29.957ms
Daniel
  • 2,355
  • 9
  • 23
  • 30
fahad pirzada
  • 103
  • 10

1 Answers1

0

try

String url = "http://10.0.2.2:8000";

and

@GET("/try/salma/123")
  • still getting lots of error . I think that somehow my android is not setting connection with my localhost through emulator – fahad pirzada Jan 20 '17 at 13:35
  • Please try to provide context with your answer. A standalone answer may solve a problem in this specific scenario, but an explanation may solve many future problems. Try to include what you think is causing the issue and why your answer will solve it. – Newd Jan 20 '17 at 14:55
  • @fahadpirzada You are right, you can't connect to server from local net. http://stackoverflow.com/questions/5224474/how-can-an-android-virtual-device-use-the-local-computers-internet – Sergey Voloshyn Jan 20 '17 at 18:55