在android中运行api调用时本地主机ip地址不起作用

时间:2018-12-24 09:22:40

标签: android localhost

您好,以下内容未收到服务器的任何回复。我是不接受的localhost IP地址。

我正在尝试检查我的ipconfig并使用本地ip地址,但无法正常工作

调用api网关时出现错误。 如果仅检查条件,否则条件不起作用

I/>>>SchedulerActivity: Sending---http://192.168.0.30retrofit2.Retrofit$1@b357456
    light_id:01
    intensity:android.support.v7.widget.AppCompatTextView{1fb8ecd7 V.ED.... ........ 30,0-41,28 #7f08010f app:id/txt_bottom61}
    cct:8
W/System.err: java.net.ConnectException: Failed to connect to /192.168.0.30:80
W/System.err:     at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:187)
        at okhttp3.internal.io.RealConnection.buildConnection(RealConnection.java:170)
        at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
        at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:187)
        at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123)
        at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93)
        at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
        at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
        at okhttp3.RealCall.getResponse(RealCall.java:243)
        at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
        at okhttp3.RealCall.access$100(RealCall.java:30)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)

private void getCCTAndIntensityValuesForPreset(){

    url = "http://192.168.0.30:9000";

    Retrofit retrofit = null;
    Log.d("123", "retrofit");

    if (retrofit == null) {
        retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).build();
        Log.d("123", "build();");
    }
    API1 service = retrofit.create(API1.class);

    Call<GetScheduler> call = service.getSchedulerData();
    Log.i(TAG, "Sending---" + url + service + "\n" + "light_id:" + lightid + "\n" + "intensity:" + dValue1 + "\n" + "cct:" + dayvalue3);

    call.enqueue(new Callback<GetScheduler>() {
        @Override
        public void onResponse(Call<GetScheduler> call, Response<GetScheduler> response) {
            if (response != null && response.isSuccessful()) {
                String getLightId = response.body().getData().getLight_id();
                if (getLightId == "00") {
                    simpleSwitch.setOn(false);
                } else {
                    simpleSwitch.setOn(true);
                }

                Toast.makeText(getApplicationContext(), "Light Id" + getLightId, Toast.LENGTH_LONG).show();
                //String light_id=response.body()
                if(simpleSwitch1.isOn()){
                int[] getIntensty = response.body().getData().getCct();

                for (int i : getIntensty) {
                    if(dayvalue1==6){
                        mIntenisty1.setProgress(getIntensty[0]);
                        mIntensityTitle1.setText(getIntensty[0] + " %");
                    }
                    if(dayvalue2 == 7) {
                        mIntenisty2.setProgress(getIntensty[1]);
                        mIntensityTitle2.setText(getIntensty[1] + " %");
                    }if(dayvalue3==8){
                        mIntenisty3.setProgress(getIntensty[2]);
                        mIntensityTitle3.setText(getIntensty[2] + " %");
                    }
                     if(dayvalue4==9){
                        mIntenisty4.setProgress(getIntensty[3]);
                        mIntensityTitle4.setText(getIntensty[3] + " %");

                    }
                    if(dayvalue5==10){
                        mIntenisty5.setProgress(getIntensty[4]);
                        mIntensityTitle5.setText(getIntensty[4] + " %");
                    }
                    if(dayvalue6==11){
                        mIntenisty6.setProgress(getIntensty[5]);
                        mIntensityTitle6.setText(getIntensty[5] + " %");

                    }
                    if(dayvalue7==12){
                        mIntenisty7.setProgress(getIntensty[6]);
                        mIntensityTitle7.setText(getIntensty[6] + " %");
                    }
                    if(dayvalue8==13){
                        mIntenisty8.setProgress(getIntensty[7]);
                        mIntensityTitle8.setText(getIntensty[7] + " %");
                    }
                    if(dayvalue9==14){
                        mIntenisty9.setProgress(getIntensty[8]);
                        mIntensityTitle9.setText(getIntensty[8] + " %");
                    }
                    if(dayvalue10==15){
                        mIntenisty10.setProgress(getIntensty[9]);
                        mIntensityTitle10.setText(getIntensty[9] + " %");
                    }
                    if(dayvalue11==16){
                        mIntenisty11.setProgress(getIntensty[10]);
                        mIntensityTitle11.setText(getIntensty[10] + " %");
                    }
                    if(dayvalue12==17) {
                        mIntenisty12.setProgress(getIntensty[11]);
                        mIntensityTitle12.setText(getIntensty[11] + " %");
                    }
                }
                int[] getCCT = response.body().getData().getIntensity();
                for (int i : getCCT) {
                    if (dayvalue1 == 6) {
                        mCCT1.setProgress(getCCT[0]);
                        mCCTTitle1.setText(getCCT[0] + " % ");
                    } if (dayvalue2 == 7) {
                        mCCT2.setProgress(getCCT[1]);
                        mCCTTitle2.setText(getCCT[1] + " % ");
                    } if (dayvalue3 == 8) {
                        mCCT3.setProgress(getCCT[2]);
                        mCCTTitle3.setText(getCCT[2] + " % ");
                    } if (dayvalue4 == 9) {
                        mCCT4.setProgress(getCCT[3]);
                        mCCTTitle4.setText(getCCT[3] + " % ");

                    } if (dayvalue5 == 10) {
                        mCCT5.setProgress(getCCT[4]);
                        mCCTTitle5.setText(getCCT[4] + " % ");
                    } if (dayvalue6 == 11) {
                        mCCT6.setProgress(getCCT[5]);
                        mCCTTitle6.setText(getCCT[5] + " % ");
                    } if (dayvalue7 == 12) {
                        mCCT7.setProgress(getCCT[6]);
                        mCCTTitle7.setText(getCCT[6] + " % ");
                    } if (dayvalue8 == 13) {
                        mCCT8.setProgress(getCCT[7]);
                        mCCTTitle8.setText(getCCT[7] + " % ");
                    } if (dayvalue9 == 14) {
                        mCCT9.setProgress(getCCT[8]);
                        mCCTTitle9.setText(getCCT[8] + " % ");
                    } if (dayvalue10 == 15) {
                        mCCT10.setProgress(getCCT[9]);
                        mCCTTitle10.setText(getCCT[9] + " % ");
                    } if (dayvalue11 == 16) {
                        mCCT11.setProgress(getCCT[10]);
                        mCCTTitle11.setText(getCCT[10] + " % ");
                    } if (dayvalue12 == 17) {
                        mCCT12.setProgress(getCCT[11]);
                        mCCTTitle12.setText(getCCT[11] + " % ");
                    }
                }
                }
                else{
                    int[] getIntensty = response.body().getData().getCct();

                    for (int i : getIntensty) {
                        if(dayvalue1==18){
                            mIntenisty1.setProgress(getIntensty[0]);
                            mIntensityTitle1.setText(getIntensty[0] + " %");
                        }
                        if(dayvalue2 == 19) {
                            mIntenisty2.setProgress(getIntensty[1]);
                            mIntensityTitle2.setText(getIntensty[1] + " %");
                        }if(dayvalue3==20){
                            mIntenisty3.setProgress(getIntensty[2]);
                            mIntensityTitle3.setText(getIntensty[2] + " %");
                        }
                        if(dayvalue4==21){
                            mIntenisty4.setProgress(getIntensty[3]);
                            mIntensityTitle4.setText(getIntensty[3] + " %");

                        }
                        if(dayvalue5==22){
                            mIntenisty5.setProgress(getIntensty[4]);
                            mIntensityTitle5.setText(getIntensty[4] + " %");
                        }
                        if(dayvalue6==23){
                            mIntenisty6.setProgress(getIntensty[5]);
                            mIntensityTitle6.setText(getIntensty[5] + " %");

                        }
                        if(dayvalue7==24){
                            mIntenisty7.setProgress(getIntensty[6]);
                            mIntensityTitle7.setText(getIntensty[6] + " %");
                        }
                        if(dayvalue8==1){
                            mIntenisty8.setProgress(getIntensty[7]);
                            mIntensityTitle8.setText(getIntensty[7] + " %");
                        }
                        if(dayvalue9==2){
                            mIntenisty9.setProgress(getIntensty[8]);
                            mIntensityTitle9.setText(getIntensty[8] + " %");
                        }
                        if(dayvalue10==3){
                            mIntenisty10.setProgress(getIntensty[9]);
                            mIntensityTitle10.setText(getIntensty[9] + " %");
                        }
                        if(dayvalue11==4){
                            mIntenisty11.setProgress(getIntensty[10]);
                            mIntensityTitle11.setText(getIntensty[10] + " %");
                        }
                        if(dayvalue12==5) {
                            mIntenisty12.setProgress(getIntensty[11]);
                            mIntensityTitle12.setText(getIntensty[11] + " %");
                        }

                    }
                    int[] getCCT = response.body().getData().getIntensity();
                    for (int i : getCCT) {
                        if (dayvalue1 == 18) {
                            mCCT1.setProgress(getCCT[0]);
                            mCCTTitle1.setText(getCCT[0] + " % ");
                        } if (dayvalue2 == 19) {
                            mCCT2.setProgress(getCCT[1]);
                            mCCTTitle2.setText(getCCT[1] + " % ");
                        } if (dayvalue3 == 20) {
                            mCCT3.setProgress(getCCT[2]);
                            mCCTTitle3.setText(getCCT[2] + " % ");
                        } if (dayvalue4 == 21) {
                            mCCT4.setProgress(getCCT[3]);
                            mCCTTitle4.setText(getCCT[3] + " % ");

                        } if (dayvalue5 == 22) {
                            mCCT5.setProgress(getCCT[4]);
                            mCCTTitle5.setText(getCCT[4] + " % ");
                        } if (dayvalue6 == 23) {
                            mCCT6.setProgress(getCCT[5]);
                            mCCTTitle6.setText(getCCT[5] + " % ");
                        } if (dayvalue7 == 24) {
                            mCCT7.setProgress(getCCT[6]);
                            mCCTTitle7.setText(getCCT[6] + " % ");
                        } if (dayvalue8 == 1) {
                            mCCT8.setProgress(getCCT[7]);
                            mCCTTitle8.setText(getCCT[7] + " % ");
                        } if (dayvalue9 == 2) {
                            mCCT9.setProgress(getCCT[8]);
                            mCCTTitle9.setText(getCCT[8] + " % ");
                        } if (dayvalue10 == 3) {
                            mCCT10.setProgress(getCCT[9]);
                            mCCTTitle10.setText(getCCT[9] + " % ");
                        } if (dayvalue11 == 4) {
                            mCCT11.setProgress(getCCT[10]);
                            mCCTTitle11.setText(getCCT[10] + " % ");
                        } if (dayvalue12 == 5) {
                            mCCT12.setProgress(getCCT[11]);
                            mCCTTitle12.setText(getCCT[11] + " % ");

                        }
                    }
                }
            }


        }
        @Override
        public void onFailure(Call<GetScheduler> call, Throwable t) {
            t.printStackTrace();
        }


    });


}

1 个答案:

答案 0 :(得分:0)

如果服务器在本地主机上运行,​​则需要使用Android代码中的地址http://10.0.2.2。 Android模拟虚拟机,该虚拟机与您的日常操作系统不同,因此无法以常规方式访问该计算机。此地址已保留,供android操作系统通过localhost与您的托管操作系统进行通信。