显示无法发送请求给服务器,无法发送GET/POST请求到服务器

我试图发送数据到服务器并读取服务器消息作为响应。后端在PHP中,工作正常。但是,无论何时我试图从android发送数据,$_GET或$_POST或$_RESPONSE阵列都将保持为空。无法发送GET/POST请求到服务器

的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代码:

$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}

2016-02-27

learner

+0

如果您通过JSON发送数据而不是后置字段,那么它的数据将不在$ _POST中。你必须从stdin中读取它。 –

+0

基本上我想发送$ _POST数组。请引导我一样。 –

+2

不知道Android,所以我不能指导你。但我可以告诉你,它看起来像你发送数据作为application/json。如果是这样的话,那么你需要在PHP中正确读入数据或以不同的方式发送数据。 –

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值