MYSQLデータベースとアンドロイドアプリの接続方法のチュートリアルを見てから、小さなアプリを開発しました。私はアンドロイドエミュレータでこのアプリをテストしていますが、これまでのところ、間違ったことが分かっていません。
私はこの問題に関して、stackoverflow全体を検索しましたが、これまでのところ成功していません。
Stackoverflowには 'using 10.0.2.2:8080
'、 'using your own ip address
'、 'switching off firewall settings
'、 'changing apache server (configuration) settings
'などのソリューションが含まれていますが、どれも私のために働いていません。ここで
アンドロイドスタジオ経由でAndroidエミュレータのlocalhost(wampserver)にアクセスする方法
は私のコードです:
BackgroundWorker.java
package apps.harry.mysqldemo;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
* Created by harry on 18/11/2017.
*/
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
Log log;
int a=0;
BackgroundWorker (Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params)
{
String type = params[0];
String login_url = "http://10.0.2.2:8081/login.php";
if(type.equals("login")) {
try {
a=1;//for debugging purposes
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();// this line seems to be some problem as during debugging it never crossed this line and hence you will see in logcat "erro is beacuse null 1", not "2"
a=2;//for debugging purposes
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null)
{
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//a=3;
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
log.e("onpostexecute","bg");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
log.e("onpostexecute","erro is beacuse "+result+" "+a);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
MainActivity.java
package apps.harry.mysqldemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity
{
EditText uname_et,pass_et;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname_et=findViewById(R.id.uname_et);
pass_et=findViewById(R.id.pass_et);
}
public void onLogin(View view)
{
String user_name=uname_et.getText().toString();
String pass_word=pass_et.getText().toString();
String type="login";
BackgroundWorker backgroundWorker=new BackgroundWorker(this);
backgroundWorker.execute(type,user_name,pass_word);
}
}
私はlogcatにこれらの行を見つけ、デバッグプロセス中
、表情があります。
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
11-20 17:55:10.604 6165-6189/apps.harry.mysqldemo W/System.err: java.net.ConnectException: Failed to connect to /10.0.2.2:8081
11-20 17:55:10.604 6165-6189/apps.harry.mysqldemo W/System.err: at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)
11-20 17:55:10.604 6165-6189/apps.harry.mysqldemo W/System.err: at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
11-20 17:55:10.604 6165-6189/apps.harry.mysqldemo W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
11-20 17:55:10.604 6165-6189/apps.harry.mysqldemo W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
11-20 17:55:10.604 6165-6189/apps.harry.mysqldemo W/System.err: at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
11-20 17:55:10.604 6165-6189/apps.harry.mysqldemo W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
11-20 17:55:10.604 6165-6189/apps.harry.mysqldemo W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
11-20 17:55:10.604 6165-6189/apps.harry.mysqldemo W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
11-20 17:55:10.604 6165-6189/apps.harry.mysqldemo W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
11-20 17:55:10.604 6165-6189/apps.harry.mysqldemo W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:258)
11-20 17:55:10.605 6165-6189/apps.harry.mysqldemo W/System.err: at apps.harry.mysqldemo.BackgroundWorker.doInBackground(BackgroundWorker.java:54)
11-20 17:55:10.605 6165-6189/apps.harry.mysqldemo W/System.err: at apps.harry.mysqldemo.BackgroundWorker.doInBackground(BackgroundWorker.java:25)
11-20 17:55:10.605 6165-6189/apps.harry.mysqldemo W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
11-20 17:55:10.605 6165-6189/apps.harry.mysqldemo W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
11-20 17:55:10.606 6165-6189/apps.harry.mysqldemo W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
11-20 17:55:10.606 6165-6189/apps.harry.mysqldemo W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
11-20 17:55:10.606 6165-6189/apps.harry.mysqldemo W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
11-20 17:55:10.606 6165-6189/apps.harry.mysqldemo W/System.err: at java.lang.Thread.run(Thread.java:764)
11-20 17:55:10.747 6165-6165/apps.harry.mysqldemo E/onpostexecute: erro is beacuse null 1
と、Failed to connect to /10.0.2.2:8081
だけでなく、それはまた、 Failed to connect to /localhost:8081
とFailed to connect to /192.168.0.5:8081
私は本当に解決策から外れています。私は何をすべきかわからない。
私を助けてください。