gpt4 book ai didi

java - 警告 : php_network_getaddresses: getaddrinfo failed: No such host is known

转载 作者:行者123 更新时间:2023-12-01 12:27:21 26 4
gpt4 key购买 nike

我大约 2 小时前问过这个问题:Error parsing data : JSONException: Value <br of type java.lang.String cannot be converted to JSONObject ,

尝试了一些解决方案后,我发现错误出在php文件中。

问题是我不太擅长 PHP...任何人都可以帮助我。

上一个问题的要点:

我正在制作注册表。我有两个类: ClientServer.javaJSONParser.java

ClientServer.java:

package com.example.mapsdemo;        
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;



import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Clientserver extends Activity {
EditText username,email,password;
Button signup,intent;
String mpostusername,mpostpassword,mpostemail;

private static final String TAG_SUCCESS = "success";
JSONParser jsonParser = new JSONParser();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clientserver);

username = (EditText) findViewById(R.id.username);

email = (EditText) findViewById(R.id.email);

password = (EditText) findViewById(R.id.password);

signup = (Button)findViewById(R.id.signup);

intent = (Button)findViewById(R.id.go);

//Setup the Button's OnClickListener
signup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Get the data
new CreateNewAccount().execute();

Log.d("my", "in onclick...create new account executed");
}
});

intent.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Clientserver.this,MainActivity.class);
startActivity(i);
}
});
}

class CreateNewAccount extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */

/**
* Creating product
* */
protected String doInBackground(String... args) {

Log.d("my", "in doInBackground()");

mpostusername = username.getText().toString();
mpostemail = email.getText().toString();
mpostpassword = password.getText().toString();


Log.d("my", "getting inputs is executed");
List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("username",mpostusername));
params.add(new BasicNameValuePair("email",mpostemail));
params.add(new BasicNameValuePair("password",mpostpassword));
Log.d("my", "parameter adding executed");

JSONObject json = jsonParser.makeHttpRequest("http://<ip_address>/android_connect/register.php","POST", params);

if(json!=null){
// do something
Log.d("my", "http request made successfully");

try {
int success = json.getInt(TAG_SUCCESS);
Log.d("my", "JSON tag_success is working");
if (success == 1) {
Log.d("my", "in success=1");
// successfully created product
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);

// closing this screen
finish();
} else {
Log.d("my", "In else");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
}
}

JSONParser.java:

package com.example.mapsdemo;    
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {

// check for request method
if(method.equals("POST")){
// request method is POST
// defaultHttpClient
Log.d("my", "method equals POST is working");
DefaultHttpClient httpClient = new DefaultHttpClient();

Log.d("my", "HTTp client is working");
HttpPost httpPost = new HttpPost(url);
Log.d("my", "HTTp post is working");
httpPost.setEntity(new UrlEncodedFormEntity(params));
Log.d("my", "url encoded");
HttpResponse httpResponse = httpClient.execute(httpPost);
Log.d("my", "HTTp response is working");
HttpEntity httpEntity = httpResponse.getEntity();
Log.d("my", "HTTp entity is working");
is = httpEntity.getContent();
Log.d("my", "getcontent is working");
}else if(method.equals("GET")){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;
}
}

db_connect.php

<?php
$connection = mysql_connect('http://xxx.xxx.xxx.xx/phpmyadmin', 'xxxx', 'xxxxx');
if (!$connection){
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db('accountsb');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}

注册.php

<?php
require('db_connect.php');
$response = array();
$username= $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];

$query = "INSERT INTO `account` (username,email,password) VALUES ('$username', '$email', '$password')";
$result = mysql_query($query);
if($result){
$response["success"] = 1;
$response["message"] = "account successfully created.";
// echoing JSON response
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";

// echoing JSON response
echo json_encode($response);
}
?>

错误:

0-06 12:16:45.340: E/JSON Parser(8630): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

这是我的 logcat 过滤日志:

10-06 12:31:09.800: D/my(11813): in onclick...create new account executed
10-06 12:31:09.800: D/my(11813): in doInBackground()
10-06 12:31:09.800: D/my(11813): getting inputs is executed
10-06 12:31:09.800: D/my(11813): parameter adding executed
10-06 12:31:09.800: D/my(11813): method equals POST is working
10-06 12:31:09.800: D/my(11813): HTTp client is working
10-06 12:31:09.800: D/my(11813): HTTp post is working
10-06 12:31:09.800: D/my(11813): url encoded
10-06 12:31:12.090: D/my(11813): HTTp response is working
10-06 12:31:12.090: D/my(11813): HTTp entity is working
10-06 12:31:12.090: D/my(11813): getcontent is working

注意:1.我在SO上看到了很多类似的问题,并尝试了很多解决方案,但尝试了大约2小时后没有成功所以,请不要标记为dulicate.plz帮助。

  • 我知道我的 php 代码遭受了 SQL 注入(inject)。但是,这只是为了练习,我将来会用 mysqli 和 PDO 编辑它们。
  • 更新:我的 php 文件中出现错误...当我尝试打开 http://xxx.xxx.xxx.xx/android_connect/register.php 时。

    它显示以下警告:

    enter image description here

    有 3 个错误,其中 2 个是警告:

    mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\android_connect\db_connect.php on line 2

    mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\android_connect\db_connect.php on line 2

    最佳答案

    OP 已要求:

    问题是您尝试连接到输入错误的主机。您只需使用主机的 IP:PORT 进行连接。

    您还需要将连接作为 mysql_select_db 函数的 2° 参数传递。

    更多语法请参阅:http://php.net/manual/en/function.mysql-connect.php

    祝你有美好的一天。

    关于java - 警告 : php_network_getaddresses: getaddrinfo failed: No such host is known,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26212709/

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