从Android我的应用程序连接MySql数据库,它不与php文件连接显示错误

尼古拉·阿沙富杜杜拉(NI Md Ashafuddula)

我想连接到Android中的MySQL数据库,但是我尝试了一下却无法正常工作。我的PHP文件还可以,我已经检查了很多次。当我单击登录按钮时收到的错误消息是此错误消息。

Exception: failed to connect to /127.0.0.1(port80):connect failed: ECONNREFUSED(Connection Refused)

这是我的代码,用于与PHP文件连接以插入数据

package com.broken.nezamulislamar.mysqlblog_1;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class SigninActivity  extends AsyncTask<String,Void,String>{
private TextView statusField,roleField;
private Context context;
private int byGetOrPost = 0;

//flag 0 means get and 1 means post.(By default it is get.)
public SigninActivity(Context context,TextView statusField,TextView roleField,int flag) {
    this.context = context;
    //this.statusField = statusField;
    //this.roleField = roleField;
    byGetOrPost = flag;
}

protected void onPreExecute(){

}

@Override
protected String doInBackground(String... arg0) {
    if(byGetOrPost == 0){ //means by Get Method

        try{
            String username = (String)arg0[0];
            String password = (String)arg0[1];
            String link = "http://127.0.0.1/Client    /jakir.php?username="+username+"& password="+password;

            URL url = new URL(link);
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(link));
            HttpResponse response = client.execute(request);
            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuffer sb = new StringBuffer("");
            String line="";

            while ((line = in.readLine()) != null) {
                sb.append(line);
                break;
            }
            in.close();
            return sb.toString();
        }

        catch(Exception e){
            return new String("Exception: " + e.getMessage());
        }
    }
    else{
        try{
            String username = (String)arg0[0];
            String password = (String)arg0[1];

            String link="http://127.0.0.1/Client/jakir.php?";
            String data  = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
            data += "&" + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");

            URL url = new URL(link);
            URLConnection conn = url.openConnection();

            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

            wr.write( data );
            wr.flush();

            BufferedReader reader = new BufferedReader(new   InputStreamReader(conn.getInputStream()));

            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while((line = reader.readLine()) != null)
            {
                sb.append(line);
                break;
            }
            return sb.toString();
        }
        catch(Exception e){
            return new String("Exception: " + e.getMessage());
        }
    }
 }

@Override
protected void onPostExecute(String result){
    this.statusField.setText("Login Successful");
    this.roleField.setText(result);
}
}
板球运动员

您已连接到本地主机。IE您的Android设备,而不是服务器。

您可以使用ipconfigifconfig或您的操作系统使用的任何方式在本地网络上查找服务器的ipv4。

例如,也许 192.168.1.100

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我想将我的Java Web应用程序与mysql数据库连接但出现错误

如何使用Php将Android应用程序连接到Amazon RDS MySQL数据库?

将我的vb.net应用程序连接到MySQL数据库

如何将数据库连接到我的Android应用程序?

将静态数据库连接到我的 android 应用程序

如何使用Python而不是PHP将ios应用程序与mysql数据库连接

如何从Android应用程序连接到远程MySQL数据库

Android应用程序可以直接连接到在线mysql数据库吗

Heroku上的数据库连接错误:将Web应用程序连接到Postgresql

如何配置此database.json文件,以便我的应用程序的api连接到数据库

C ++应用程序MySQL odbc数据库连接错误:抛出'otl_tmpl_exception <>实例后终止调用

尝试将Play应用程序连接到mysql数据库时出现SQL语法错误

我了解如何使用C#Windows Form应用程序连接Web mysql数据库

我的 Spring Boot 应用程序容器无法连接到我的 postgresql 数据库

将 Angular 6 应用程序连接到 DreamHost 上托管的 php/mysql 数据库的服务

在独立应用程序中处理数据库连接

Windows Phone 8.1应用程序与数据库连接

Rails应用程序与Advantage数据库的连接

无法将应用程序连接到数据库

Mongodb数据库连接代码中断应用程序

Windows窗体应用程序中的数据库连接

应用程序和数据库连接

Logcat 显示我为我的 Android 应用程序创建的 SQLite 数据库的各种类型的错误

与连接PostgreSQL数据库春季启动应用程序错误

将我的应用程序连接到SQLite数据库-如何?

如何将我的应用程序与数据库服务器连接

无法从我的Django Web应用程序连接到Amazon RDS Postgres数据库

我的 MVC 应用程序无法识别数据库连接

无法使用我的heroku应用程序连接到mongodb数据库?