Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓应用程序可以';不要发布到url_Android - Fatal编程技术网

Android 安卓应用程序可以';不要发布到url

Android 安卓应用程序可以';不要发布到url,android,Android,我正在为一个学校项目开发一个相对简单的登录应用程序。我在将我的应用程序连接到任何URL(本地或其他)时遇到问题。我已经将互联网访问权限添加到android清单文件中 <uses-permission android:name="android.permission.INTERNET" /> 以下代码位于我的主要活动中: public class MainActivity extends Activity { private EditText username=null

我正在为一个学校项目开发一个相对简单的登录应用程序。我在将我的应用程序连接到任何URL(本地或其他)时遇到问题。我已经将互联网访问权限添加到android清单文件中

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

以下代码位于我的主要活动中:

public class MainActivity extends Activity {
    private EditText  username=null;
    private EditText  password=null;
    private Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = (EditText)findViewById(R.id.ucidText);
        password = (EditText)findViewById(R.id.passText);
        login = (Button)findViewById(R.id.button1);


//URL CAN BE ANYTHING, WOULD NORMALLY BE 192.168.1.102
        if(isConnectedToServer("<-URL to page on locally hosted server->",10)){
            Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(getApplicationContext(), "Connection failed", Toast.LENGTH_SHORT).show();
        }
    }


//Function to determine if connection exists
    public boolean isConnectedToServer(String url, int timeout) {
        try{
            URL myUrl = new URL(url);    //<--------- I believe the issue has to do with this line. 
            URLConnection connection = myUrl.openConnection();
            connection.setConnectTimeout(timeout);
            connection.connect();
            return true;
        } catch (Exception e) {         
            return false;
        }
    }   
}
公共类MainActivity扩展活动{
私有EditText用户名=null;
私有EditText密码=null;
私人按钮登录;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
用户名=(EditText)findViewById(R.id.ucidText);
密码=(EditText)findViewById(R.id.passText);
登录=(按钮)findViewById(R.id.button1);
//URL可以是任何内容,通常为192.168.1.102
如果(已连接到服务器(“,10)){
Toast.makeText(getApplicationContext(),“Connected”,Toast.LENGTH\u SHORT.show();
}
否则{
Toast.makeText(getApplicationContext(),“连接失败”,Toast.LENGTH\u SHORT.show();
}
}
//函数来确定连接是否存在
公共布尔值已连接到服务器(字符串url,int超时){
试一试{

URL myUrl=newurl(URL);//您应该在“URL”字符串中使用一些协议

例如,您可以使用

 "http://serverIP" 
其中serverIP为192.168.1.102,端口默认为80


我们无法使用“192.168.1.102”作为url,因为url类不能忽略协议。

您应该在“url”字符串中使用一些协议

例如,您可以使用

 "http://serverIP" 
其中serverIP为192.168.1.102,端口默认为80


我们不能使用“192.168.1.102”作为url,因为无法忽略url类的协议。

由于在主线程上执行网络请求,因此出现了android.os.NetworkOnMainThreadException。您不应该这样做,因为这样会阻止主线程。相反,您可以启动一个新线程来执行网络请求,如下所示:

final Handler handler = new Handler();
new Thread(){
     public void run(){
       // check network connection here.
       handler.post(/* do the toast work in a Runnable as the parameter here*/);
    }
}.start();

您获得了android.os.NetworkOnMainThreadException,因为您在主线程上执行网络请求。您不应该这样做,因为您将阻止主线程。相反,您可以启动一个新线程来执行网络请求,如下所示:

final Handler handler = new Handler();
new Thread(){
     public void run(){
       // check network connection here.
       handler.post(/* do the toast work in a Runnable as the parameter here*/);
    }
}.start();

运行此代码时会发生什么情况?是否会出现任何错误?如果是,错误是什么?
catch(异常e){return false;}
这可能是一个非常糟糕的主意。如果出现任何问题,您只需忽略异常提供的许多详细信息即可找出问题所在。除非包含抛出声明,否则URL对象将无法运行。e.printStackTrace()返回java.net.MalformedURLException:Protocol not found“URL”这是否意味着我必须指定一个http协议?您提供的确切URL是什么?请注意,它必须格式正确。这意味着您必须提供一些协议,例如“http”(但它可以是任何有效的协议…您没有提供足够的信息让我们提出任何建议)。以前,当我将其更改为
192.168.1.102/cstest/index.php时,我的url只是服务器的IP地址http://192.168.1.102/cstest/index.php
应用程序崩溃了,我收到了以下信息。如果我没有一次提供所有信息,我很抱歉,我是Android新手,仍在学习,所以我不知道要找什么当你运行这个代码时会发生什么?你会得到任何错误吗?如果是,它们是什么?
catch(异常e){returnfalse;}
这可能是一个非常糟糕的主意。如果出现任何问题,您只需忽略异常提供的许多详细信息即可找出问题所在。除非包含抛出声明,否则URL对象将无法运行。e.printStackTrace()返回java.net.MalformedURLException:Protocol not found“URL”这是否意味着我必须指定一个http协议?您提供的确切URL是什么?请注意,它必须格式正确。这意味着您必须提供一些协议,例如“http”(但它可以是任何有效的协议…您没有提供足够的信息让我们提出任何建议)。以前,当我将其更改为
192.168.1.102/cstest/index.php时,我的url只是服务器的IP地址http://192.168.1.102/cstest/index.php
应用程序崩溃了,我收到了以下信息。如果我没有一次提供所有信息,我很抱歉,我是Android新手,仍在学习,所以我不知道要找什么ly.当我添加
http://
时,应用程序崩溃,出现以下问题。当我添加
http://
时,应用程序崩溃,出现以下问题。@Phreakradio是的,AsyncTask也可以解决您的问题。@Phreakradio是的,AsyncTask也可以解决您的问题。