Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.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 无法连接到网站_Android - Fatal编程技术网

Android 无法连接到网站

Android 无法连接到网站,android,Android,我正在开发一个需要用户登录的应用程序。它不是在果冻豆中起作用,而是在姜饼中起作用。有什么想法吗 这是代码 MainActivity.java: package com.example.test; public class MainActivity extends Activity { String sname,spass; EditText sname1,spass1; @Override protected void onCreate(Bundle save

我正在开发一个需要用户登录的应用程序。它不是在果冻豆中起作用,而是在姜饼中起作用。有什么想法吗

这是代码

MainActivity.java:

package com.example.test;

public class MainActivity extends Activity {

    String sname,spass;
    EditText sname1,spass1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sname1 = (EditText) findViewById(R.id.editText1);
        spass1 = (EditText) findViewById(R.id.editText2);
        Button login=(Button) findViewById(R.id.button1);

        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sname   = sname1.getText().toString();
                spass   = spass1.getText().toString();

                BufferedReader reader=null;
                try
                    { 


                  String data= "&" + URLEncoder.encode("sname", "UTF-8") + "=" + URLEncoder.encode(sname, "UTF-8"); 
                  data += "&" + URLEncoder.encode("spass", "UTF-8") + "=" + URLEncoder.encode(spass, "UTF-8"); 


                  String text = null;
                  String temp1=null;

                  // Send data 



                URL url = new URL("http://10.0.2.2/login.php");

                  URLConnection conn = url.openConnection(); 
                  conn.setDoOutput(true); 
                  OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
                  wr.write(data); 
                  wr.flush(); 
                // Get the response 


                reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    String line = null;


                    while((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    text = sb.toString();
                    temp1=text.trim();


                    if(temp1.contains("notequal"))
                     {
                     Toast.makeText(getApplicationContext(),"Wrong Code or Id", Toast.LENGTH_SHORT).show();
                     }

                    else if(temp1.contains("equalto"))
                     {
                        Toast.makeText(getApplicationContext()," Login Successful", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(MainActivity.this, Second.class));
                     }


                    }

                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
                finally
                {
                    try
                    {

                        reader.close();
                    }
                    catch(Exception ex) {ex.printStackTrace();}
                }

            }
        });//login



    }//oncreate
}//main
以下是activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="22dp"
    android:text="Name" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_marginLeft="21dp"
    android:layout_toRightOf="@+id/textView1"
    android:ems="10" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="16dp"
    android:text="Password" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_toRightOf="@+id/textView3"
    android:ems="10" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="17dp"
    android:text="Login" />
下面是login.php文件:

<?php

   $sname   = urldecode($_POST['sname']);
   $spass   = urldecode($_POST['spass']);

 mysql_connect("localhost","root","");
 mysql_select_db("paper");
 $sql=mysql_query("select * from student where name='$sname' and password='$spass'");
 if($row=mysql_fetch_assoc($sql)){
 echo "equalto"; 

 }
else{
echo "notequal";}
 mysql_close();
?>

在这里,您的问题的解决方案是使用AsyncTask进行web请求,我使用HttpClient然后使用HttpPost在web上进行请求

//

新异步任务{

                    @Override
                    protected Integer doInBackground(Void... params) {
                        // TODO Auto-generated method stub


                        try
                        {
                            HttpClient client = new DefaultHttpClient();
                            HttpPost httppost = new HttpPost("http://182.188.140.167/gpsweb/check%20username.php");
                            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                            nameValuePairs.add(new BasicNameValuePair("username",username));
                            nameValuePairs.add(new BasicNameValuePair("password",password));

                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                            HttpResponse response = client.execute(httppost); 
                            HttpEntity resEntityGet = response.getEntity();  
                            if (resEntityGet != null) 
                            { 
                                //Log.e("Checking responce", EntityUtils.toString(resEntityGet).toString());
                                String response1 = EntityUtils.toString(resEntityGet).toString();
                                if(response1.contains("1"))
                                {
                                    Intent i=new Intent(MainActivity.this,GetLocation.class);
                                    Bundle b=new Bundle();
                                    b.putString("uname",uname);
                                    i.putExtras(b);
                                    startActivity(i);
                                }
                                else
                                {
                                    return 0;
                                }
                            }
                        }
                        catch(Exception ex)
                        {
                                ex.printStackTrace();
                        }



                        return 2;
                    }

您可能需要在清单中添加相应的权限

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

它的服务u hv写得对吗?它不像是在一个版本上工作而不是在另一个版本上工作…它一定有不同的原因…你使用了s ame凭据吗?…检查来自两个设备的php日志你在主线程上访问网络。它在JB设备上受到限制。请检查以下线程:我已经开发了一个完整的应用程序阳离子基于这种方法,即Url对象和使用URLConnection conn等。它在姜饼中运行良好,但在果冻豆中不起作用。我也尝试过使用AsyncTask。在姜饼中运行,但在果冻豆中不起作用。我使用果冻豆的目标版本从头开始创建了整个项目,但没有用…如果你同意,我应该将我的应用程序发送给你吗电子邮件您的邮件ID异常是java.net.socketException..有人能得到它吗?仍然不工作..说错误java.net.socketException权限被拒绝否。我已经添加了该权限。如果不是这样,它将不会在姜饼上运行