我正在创建一个Android应用程序,我使用WAMP服务器实现了客户端服务器。我创建了我的php文件,服务器正在运行,我的php文件的简单版本将数据成功插入到我的mysql数据库中。在我的应用程序中,我包含了一个AsyncTask
java文件,以便插入我的数据。我确定我的url字符串是错误的,这就是它打印以下错误的原因:
java.net.ConnectException:无法连接到localhost / 127.0.0.1(端口80):连接失败:ECONNREFUSED(拒绝连接)
我在三星S4上测试我的应用程序,所以没有模拟器回复将我的ip字符串更改为10.0.2.2。我使用localhost,127.0.0.1和我的IP地址,我通过在cmd中运行ipconfig找到...没有任何作用,它正在打印相同的错误。我读了15-20个类似的问题,但我仍然不知道该怎么做。
我的清单上存在互联网权限,我的手机通过路由器中的Wi-Fi连接。 (我也尝试使用我的手机4G,这也不起作用。)
您可以在下面找到我的代码:
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class BackgroundTask extends AsyncTask<String, Void, String>
{
Context ctx;
BackgroundTask(Context ctx)
{
this.ctx = ctx;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected String doInBackground(String... params)
{
String create_url = "http://217.137.144.65/myapp/create.php";
String method = params[0];
if(method.equals("create"))
{
String name = params[1];
try
{
URL url = new URL(create_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
String data = URLEncoder.encode("exCatName","UTF-8") + "=" + URLEncoder.encode(name,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
return "Category Created Successfully";
}
catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values)
{
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result)
{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
public class CreateNewCategoryActivity extends Activity
{
private ImageView backImageView, saveImageView;
private EditText nameEditText;
String categoryName;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_new_category);
backImageView = (ImageView)findViewById(R.id.backImageView);
saveImageView = (ImageView)findViewById(R.id.saveImageView);
nameEditText = (EditText)findViewById(R.id.nameEditText);
backImageView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
backMethod();
}
});
saveImageView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
createNewCategoryMethod();
}
});
}
public void createNewCategoryMethod()
{
categoryName = nameEditText.getText().toString();
String method = "create";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method, categoryName);
finish();
}
public void backMethod()
{
//startActivity(new Intent(getApplicationContext(), BalanceActivity.class));
finish();
}
}
php文件:
<?php
require "init.php";
$exCategories_ID = "1";
$exCatName = $_POST["name"];
$sql_query = "insert into Expense_Categories values
('$exCategories_ID','$exCatName');";
if(mysqli_query($con,$sql_query))
{
echo "<h3>Data Insertion Success...</h3>";
}
else
{
echo "Data insertion error...".mysqli_error($con);
}
?>
答案 0 :(得分:1)
java.net.ConnectException:无法连接到localhost / 127.0.0.1 (端口80):连接失败:ECONNREFUSED(拒绝连接)
String create_url = "http://localhost/myapp/create.php";
错了,正确的是
String create_url = "http://192.168.0.3/myapp/create.php";
192.168.0.3
你的电脑(服务器)的本地IP
答案 1 :(得分:0)
我遇到了同样的问题。我的电脑上安装了MySQL
,然后我安装了WAMP
。然后问题就出现了。包含mysql端口号的WAMP和我的PC的MySQL是冲突的。
因此,您可以先卸载PC的MySQL,然后重启并使用WAMP的MySQL。然后问题就会解决。
答案 2 :(得分:0)
要访问 localhost
,您应该为 localhost
使用本地 IPv4 + 服务器端口。
如何获得这个 IPv4: https://www.whatismybrowser.com/detect/what-is-my-local-ip-address
KOTLIN 异步请求:
private val client = OkHttpClient()
fun run() {
val request = Request.Builder()
.url("http://192.168.0.104:5000/api/post/latest")
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
response.use {
if (!response.isSuccessful) throw IOException("Unexpected code $response")
for ((name, value) in response.headers) {
println("$name: $value")
}
println(response.body!!.string())
}
}
})
}
安装 OkHttp:https://square.github.io/okhttp/