Android设备与本地API服务器通信

时间:2015-10-27 06:03:28

标签: android cordova android-studio

我想知道如何在外部设备上模拟我的Android应用,以便与在我的机器上运行的本地API服务器进行通信。

该应用正在使用cordova并使用USB连接在Android设备上运行。我在本地运行API服务器。

使用我机器的IP地址,我试图通过应用程序内的HTTP调用与API服务器通信。但是我在调​​试器中遇到404错误。

POST http://myIP:8083/api/login 404 (Not Found)

API服务器正在运行且可访问。我通过设备的网络浏览器访问它确认了它。我的预感是我必须在某处将我的IP列入白名单。但我不确定。有什么建议吗?

使用Cordova 5.3.3。在LGE Nexus 5 Android 4.4.4(API 19)上进行测试。

1 个答案:

答案 0 :(得分:0)

使用https://github.com/apache/cordova-plugin-whitelist插件并在<meta http-equiv="Content-Security-Policy" ...> <head>文件的index.html中添加<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background" > <LinearLayout android:id="@+id/layoutBehind" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_weight="11" android:background="@color/black" android:isScrollContainer="false" > <include layout="@layout/actionbar" > </include> </LinearLayout> <RelativeLayout android:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#F0F0F0" android:orientation="vertical" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/li2" android:isScrollContainer="false" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical" > <LinearLayout style="@style/ln_inserttext" android:layout_width="match_parent" > </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" > <TextView android:id="@+id/tv_name" style="@style/tv_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Name" /> <com.decopcard.layout.EditTextDone android:id="@+id/edt_name" style="@style/edt_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/edittext_backgrounf" android:focusableInTouchMode="true" android:gravity="top" android:lines="2" android:scrollbarStyle="insideInset" android:scrollbars="vertical" android:text="@string/TextDefaultName" android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" android:textStyle="bold" /> <TextView android:id="@+id/tv_name_divide" style="@style/rl_package2" android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#F0ECE0" /> <TextView android:id="@+id/tv_phonetic" style="@style/tv_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Phonetic" /> <com.decopcard.layout.EditTextDone android:id="@+id/edt_phone_tic" style="@style/edt_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/edittext_backgrounf" android:gravity="top" android:lines="2" android:scrollbarStyle="insideInset" android:scrollbars="vertical" android:text="@string/TextDefaultName" android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" android:textStyle="bold" /> </LinearLayout> <LinearLayout style="@style/custom_dialog_remainpoint_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" > <TextView android:id="@+id/tv_zip" style="@style/tv_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Zip" /> <com.decopcard.layout.EditTextDone android:id="@+id/edt_zip" style="@style/edt_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/edittext_backgrounf" android:gravity="top" android:lines="2" android:scrollbarStyle="insideInset" android:scrollbars="vertical" android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" android:textStyle="bold" /> <TextView android:id="@+id/tv_zip_divide" style="@style/tv_insert_text" android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#F0ECE0" /> <TextView android:id="@+id/tv_address" style="@style/tv_inserttext_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Address" /> <EditText android:id="@+id/edt_address" style="@style/edt_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/edittext_backgrounf" android:gravity="top" android:lines="3" android:scrollbarStyle="insideInset" android:scrollbars="vertical" android:text="@string/TextDefaultAddress" android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" android:textStyle="bold" /> <TextView android:id="@+id/tv_address_divide" style="@style/tv_insert_text" android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#F0ECE0" /> <TextView android:id="@+id/tv_phone_number" style="@style/rl_package2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/PhoneNumber" /> <com.decopcard.layout.EditTextDone android:id="@+id/edt_phone_number" style="@style/edt_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/edittext_backgrounf" android:gravity="top" android:lines="2" android:scrollbarStyle="insideInset" android:scrollbars="vertical" android:text="@string/TextDefaultPhoneNumber" android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" android:textStyle="bold" /> </LinearLayout> <LinearLayout style="@style/custom_dialog_remainpoint_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" > <TextView android:id="@+id/tv_mail" style="@style/tv_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/MailAddress" /> <com.decopcard.layout.EditTextDone android:id="@+id/edt_mail" style="@style/edt_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/edittext_backgrounf" android:gravity="top" android:lines="2" android:scrollbarStyle="insideInset" android:scrollbars="vertical" android:text="@string/TextDefaultMailAddress" android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" android:textStyle="bold" /> <TextView android:id="@+id/tv_divide" style="@style/rl_package2" android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#F0ECE0" /> <TextView android:id="@+id/tv_url" style="@style/tv_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Url" /> <com.decopcard.layout.EditTextDone android:id="@+id/edt_url" style="@style/edt_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/edittext_backgrounf" android:gravity="top" android:lines="2" android:scrollbarStyle="insideInset" android:scrollbars="vertical" android:text="@string/TextDefaultUrl" android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" android:textStyle="bold" /> </LinearLayout> <LinearLayout style="@style/custom_dialog_remainpoint_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" > <TextView android:id="@+id/tv_company" style="@style/tv_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Company" /> <com.decopcard.layout.EditTextDone android:id="@+id/edt_company" style="@style/edt_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/edittext_backgrounf" android:gravity="top" android:lines="2" android:scrollbarStyle="insideInset" android:scrollbars="vertical" android:text="@string/TextDefaultCompany" android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" android:textStyle="bold" /> <TextView android:id="@+id/tv_company_divide" style="@style/rl_package2" android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#F0ECE0" /> <TextView android:id="@+id/tv_position" style="@style/tv_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/CompanyPosition" /> <com.decopcard.layout.EditTextDone android:id="@+id/edt_position" style="@style/edt_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/edittext_backgrounf" android:gravity="top" android:lines="2" android:scrollbarStyle="insideInset" android:scrollbars="vertical" android:text="@string/TextDefaultCompanyPosition" android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" android:textStyle="bold" /> </LinearLayout> <LinearLayout style="@style/custom_dialog_remainpoint_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" > <TextView android:id="@+id/tv_opening" style="@style/tv_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/OpeningHours" /> <com.decopcard.layout.EditTextDone android:id="@+id/edt_open" style="@style/edt_insert_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/edittext_backgrounf" android:gravity="top" android:lines="2" android:scrollbarStyle="insideInset" android:scrollbars="vertical" android:text="@string/TextDefaultOpeningHours" android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" android:textStyle="bold" /> </LinearLayout> <LinearLayout style="@style/custom_dialog_remainpoint_1" android:layout_width="match_parent" android:layout_height="wrap_content" > </LinearLayout> </LinearLayout> </ScrollView> <LinearLayout android:id="@+id/li2" style="@style/ln_shop2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@android:color/black" android:gravity="left" android:orientation="horizontal" > <LinearLayout android:id="@+id/liner_history" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" > </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/hot" android:visibility="invisible" /> <TextView android:id="@+id/tv_middle" style="@style/text_shop2_bellow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Hot" android:textColor="@android:color/white" android:visibility="invisible" /> </LinearLayout> <LinearLayout android:id="@+id/liner_middle" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <ImageView android:id="@+id/img_middle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/btn_contact_inserttext" /> </LinearLayout> </FrameLayout> <LinearLayout android:id="@+id/liner_search" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" > </LinearLayout> </LinearLayout> </RelativeLayout> </LinearLayout> </LinearLayout> 来解决此问题。

有关详细信息,请阅读https://github.com/apache/cordova-plugin-whitelist#content-security-policy