我正在创建一个Android应用程序,我已经使用WAMP服务器实现了一个客户机服务器。我创建了我的php文件,服务器正在运行,我的php文件的一个简单版本成功地将数据插入到我的mysql数据库中。在我的应用程序中,为了插入数据,我已经包含了一个AsyncTask java文件。我确信我的url字符串是错误的,这就是它打印以下错误的原因:
java.net.ConnectException:未能连接到本地主机/127.0.0.1(端口80):连接失败: ECONNREFUSED (连接被拒绝)
我正在三星的S4上测试我的应用程序,所以没有仿真器会将我的ip字符串更改为10.0.2.2。我使用了localhost,127.0.0.1和我通过在cmd...nothing中运行ipconfig找到的IP入口,它也打印了相同的错误。我读了15-20个类似的问题,但我还是不知道该怎么办。
互联网许可存在于我的清单上,我的电话通过我的路由器中的Wi连接。(我也尝试用我的手机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);
}
?>发布于 2016-04-20 15:56:49
java.net.ConnectException:未能连接到本地主机/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是您的pc (服务器)的本地ip
发布于 2016-04-20 15:16:32
我也有同样的问题。我的PC包含安装了MySQL,然后我安装了WAMP。然后问题就出现了。包含mysql端口号的WAMP和我的PC的MySQL是相互冲突的。
所以你可以先卸载你的电脑的MySQL,然后重新启动并使用WAMP的MySQL。然后问题就解决了。
发布于 2021-01-09 18:15:27
要访问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/
https://stackoverflow.com/questions/36747820
复制相似问题