gpt4 book ai didi

java.net.ConnectException : failed to connect to localhost/127. 0.0.1(端口 80):连接失败:ECONNREFUSED(连接被拒绝)

转载 作者:行者123 更新时间:2023-11-28 23:33:06 24 4
gpt4 key购买 nike

我正在创建一个 Android 应用程序,并且我已经使用 WAMP 服务器实现了一个客户端服务器。我已经创建了我的 php 文件,服务器正在运行,我的 php 文件的一个简单版本正在成功地将数据插入到我的 mysql 数据库中。在我的应用程序中,我包含了一个 AsyncTask java 文件以插入我的数据。我确定我的 url 字符串是错误的,这就是它打印以下错误的原因:

java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)

我正在三星 S4 上测试我的应用程序,因此没有模拟器回复将我的 ip 字符串更改为 10.0.2.2。我使用了 localhost、127.0.0.1 和我通过在 cmd 中运行 ipconfig 找到的 IP 地址……没有任何效果,它正在打印相同的错误。我读了 15-20 个类似的问题,但我仍然不知道该怎么做。

我的 list 上有互联网许可,我的手机通过路由器中的 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);
}

?>

最佳答案

java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)

String create_url = "http://localhost/myapp/create.php";

错了,正确的是

String create_url = "http://192.168.0.3/myapp/create.php";

192.168.0.3你的电脑(服务器)的本地IP

关于java.net.ConnectException : failed to connect to localhost/127. 0.0.1(端口 80):连接失败:ECONNREFUSED(连接被拒绝),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36747820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com