gpt4 book ai didi

php - 无法向服务器发送 GET/POST 请求

转载 作者:行者123 更新时间:2023-11-30 01:31:44 25 4
gpt4 key购买 nike

我正在尝试向服务器发送获取数据并读取服务器消息作为响应。后端在 PHP 中并且工作正常。但是,每当我尝试从 android 发送数据时,$_GET$_POST$_RESPONSE 数组仍然为空。

doInBackground 是:

 @Override
protected Void doInBackground(Void... params) {
Log.d(TAG,"Inside do in background");

try {

Log.d(TAG,"Here is new url.");
URL url = new URL("http://192.168.1.33/post/home.php");

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();


urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/json");


//sending data in json format

JSONObject jsonObject = new JSONObject();

jsonObject.put("name","salman Khan");

jsonObject.put("password","khankhan");

String sendingString ;

sendingString = jsonObject.toString();

byte[] outputBytes = sendingString.getBytes("UTF-8");

Log.d(TAG, "Output bytes are " + sendingString);

OutputStream outputStream = urlConnection.getOutputStream();

Log.d(TAG," got output stream.");
outputStream.write(outputBytes);

Log.d(TAG,"I am waiting for response code");
int responseCode = urlConnection.getResponseCode();

Log.d(TAG,"response code: " + responseCode);

if ( responseCode == HttpURLConnection.HTTP_OK)
{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

String line;

Log.d(TAG,"Before reading the line.");

while ( (line = bufferedReader.readLine()) != null )
{
Log.d(TAG, " The line read is: " + line);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

PHP 代码:

<?php

$response = array();
// print_r($_GET);
print_r($_REQUEST);
if ( !empty($_GET['name']) || !empty($_GET['password']))
{
// echo "I am in true.";
$response['name'] = $_GET['name'];
$response['password'] = $_GET['password'];
$response['status'] = true;
}
else
{
$response['status'] = false;
}
echo json_encode($response);
?>

我收到以下日志:

964 21872-21895/com.example.khan.post_interaction D/shahjahan:  got output stream.
02-27 12:09:51.964 21872-21895/com.example.khan.post_interaction D/shahjahan: I am waiting for response code
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: response code: 200
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: Before reading the line.
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: The line read is: Array
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: The line read is: (
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: The line read is: )
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: The line read is: {"status":false}

最佳答案

检查 list 文件中添加的以下权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

关于php - 无法向服务器发送 GET/POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35666729/

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