在Android上找不到Phonegap cordova 404

时间:2016-07-25 15:52:35

标签: javascript android cordova http-status-code-404

我正在制作一个需要使用远程http服务器的应用程序。在浏览器中我对ajax / inAppBrowser / iframe没有任何问题,但是当在Android中构建和运行它时它不再起作用。

我使用了远程chrome调试器,只是注意到所有请求都没有从缓存中找到404,所以我猜测应用程序没有权利在网上发出请求。我搜索了一个解决方案,但我无法弄清楚它来自哪里。

这是我的config.xml:

07-25 20:56:53.451 9055-9055/com.example.gary.natureall E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.gary.natureall, PID: 9055
                                                                          java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                              at android.view.View.performClick(View.java:5698)
                                                                              at android.widget.TextView.performClick(TextView.java:10846)
                                                                              at android.view.View$PerformClick.run(View.java:22565)
                                                                              at android.os.Handler.handleCallback(Handler.java:739)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:158)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:7230)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                           Caused by: java.lang.reflect.InvocationTargetException
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                              at android.view.View.performClick(View.java:5698) 
                                                                              at android.widget.TextView.performClick(TextView.java:10846) 
                                                                              at android.view.View$PerformClick.run(View.java:22565) 
                                                                              at android.os.Handler.handleCallback(Handler.java:739) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                              at android.os.Looper.loop(Looper.java:158) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:7230) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                                                                           Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=com.sec.android.app.camera/.Camera } from ProcessRecord{3ef341 9055:com.example.gary.natureall/u0a10} (pid=9055, uid=10010) with revoked permission android.permission.CAMERA
                                                                              at android.os.Parcel.readException(Parcel.java:1620)
                                                                              at android.os.Parcel.readException(Parcel.java:1573)
                                                                              at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3130)
                                                                              at android.app.Instrumentation.execStartActivity(Instrumentation.java:1541)
                                                                              at android.app.Activity.startActivityForResult(Activity.java:4284)
                                                                              at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
                                                                              at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
                                                                              at android.app.Activity.startActivityForResult(Activity.java:4231)
                                                                              at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)
                                                                              at com.example.gary.natureall.uploadPicScreen.launchCamera(uploadPicScreen.java:48)
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                              at android.view.View.performClick(View.java:5698) 
                                                                              at android.widget.TextView.performClick(TextView.java:10846) 
                                                                              at android.view.View$PerformClick.run(View.java:22565) 
                                                                              at android.os.Handler.handleCallback(Handler.java:739) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                              at android.os.Looper.loop(Looper.java:158) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:7230) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

我试图从开始再次开始,但这是最简单的。有些线路可能没用,但我尝试了不同的解决方案来解决我的问题。

的index.html:

<?xml version='1.0' encoding='utf-8'?>
<widget id="foo" version="0.0.1" xmlns="http://www.w3.org/ns/widgets"    xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>foo</name>
<description>
    foo
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">

</author>
<content src="index.html" />
<plugin name="cordova-plugin-whitelist"/>
<access origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />
<access origin="*" />
<platform name="android">
    <allow-intent href="market:*" />
    <access origin="*" />
    <allow-intent href="*" />
    <allow-navigation href="*" />
</platform>
<platform name="ios">
    <allow-intent href="itms:*" />
    <allow-intent href="itms-apps:*" />
</platform>
<engine name="android"/>
<plugin name="cordova-plugin-inappbrowser"/>

index.js

<!DOCTYPE html>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at


http://www.apache.org/licenses/LICENSE-2.0


Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->
<html>
<head>
    <!--
    Customize this policy to fit your own app's needs. For more guidance, see:
        https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
    Some notes:
        * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
        * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
        * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
            * Enable inline JS: add 'unsafe-inline' to default-src
    -->
    <!-- <meta http-equiv="Content-Security-Policy" content="default-src   'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *"> -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' * ws://localhost:35729 data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; media-src *;script-src 'self' localhost:35729 'unsafe-eval' 'unsafe-inline';">
    <meta name="format-detection" content="telephone=no">
    <meta name="msapplication-tap-highlight" content="no">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <title>Hello World</title>
</head>
<body>
  <center><div class="">
      <div id="deviceready" class="row col s12">
        <h1>foo</h1>
        <div class="input-field col s8">
          <input type="text" id="addr" name="addr" placeholder="Adresse">
        </div>
        <div class="input-field  col s4">
          <input type="text" id="port" placeholder="port">
        </div>
        <div class="input-field  col s12">
          <input type="text" id="name" placeholder="Nom d'utilisateur">
        </div>
        <div class="input-field  col s12">
          <input type="password" id="pswd" placeholder="Mot de passe">
        </div>
        <button id="button_test" class="waves-effect waves-light btn blue lighten-1" value="tester">tester</button>
        <button id="button_start" class="waves-effect waves-light btn blue lighten-1" value="commencer">commencer</button>
      </div>
      <div id="result"></div>
      <div id="window-iframe"></div>
  </div></center>
  <script type="text/javascript" src="cordova.js"></script>
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <script type="text/javascript" src="js/index.js"></script>
</body>

AndroidManifest

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */




var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');


        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');


        console.log('Received Event: ' + id);
    }
};


function debug(string){
  $("#result").html($("#result").html()+"</br>"+string);
}


debug("ready");


document.getElementById("button_test").addEventListener("click", test);
document.getElementById("button_start").addEventListener("click", start);


document.getElementById("addr").value = localStorage.getItem("address");
document.getElementById("port").value = localStorage.getItem("port");
document.getElementById("name").value = localStorage.getItem("username");
document.getElementById("pswd").value = localStorage.getItem("password");


function test(){
  debug("connecting...");
  console.log("connecting...");
  addr = document.getElementById("addr").value;
  port = document.getElementById("port").value;
  username = document.getElementById("name").value;
  password = document.getElementById("pswd").value;


  url_req = "http://www.lemonde.fr/";
  // url_req = 'http://'+addr+':'+port;//'/actions.php?do=auth&login='+username+'&password='+password;
  debug("connecting...to->"+url_req)
  $.ajax({
      url: url_req,
      timeout: 30000,
      success: function(data, status, xhr) {
        debug("ajax sucess");
        console.log(data);
        data = JSON.parse(data);
        if(data.return == "success"){
          $("#result").html("OK");
        } else {
          debug("page return fail");
          $("#result").html(data.error);
        }
      },
      error: function (xhr, ajaxOptions, thrownError) {
        debug("error->"+thrownError);
      }
  });
  localStorage.setItem("address", addr);
  localStorage.setItem("port", port);
  localStorage.setItem("username", username);
  localStorage.setItem("password", password);
}


function start(){
  addr = document.getElementById("addr").value;
  port = document.getElementById("port").value;
  username = document.getElementById("name").value;
  password = document.getElementById("pswd").value;
  $("#window-iframe").html("<iframe src='http://www.lemonde.fr/' width='800px' height='600px'></iframe>");
  //var ref = window.open('http://www.lemonde.fr/', '_blank', 'location=yes');
}


app.initialize();

0 个答案:

没有答案