gpt4 book ai didi

php - 将输入数据从android发送到php

转载 作者:行者123 更新时间:2023-11-29 00:23:14 24 4
gpt4 key购买 nike

这些代码在按下按钮后执行。我正在尝试将字符串数据发送到我服务器中的 php 文件。但是在我按下按钮后应用程序停止了。我能知道这里有什么问题吗?非常感谢任何帮助:D

        HttpClient client = new DefaultHttpClient();
HttpPost hpost = new HttpPost("http://myservername.com/postTest.php");
status = mStatus.getText().toString();
event = mEvent.getText().toString();
time = mTime.getText().toString();
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("status", status));
nameValuePairs.add(new BasicNameValuePair("event", event));
nameValuePairs.add(new BasicNameValuePair("time", time));
hpost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
client.execute(hpost);
mStatus.setText("");
mEvent.setText("");
mTime.setText("");
}catch (UnsupportedEncodingException e){
Toast.makeText(this, "Unsupported Encoding Exception " + e.getMessage(), Toast.LENGTH_LONG).show();
}catch (ClientProtocolException e){
Toast.makeText(this, "Client Protocol Exception " + e.getMessage(), Toast.LENGTH_LONG).show();
}catch (IOException e){
Toast.makeText(this, "IO Exception "+e.getMessage(), Toast.LENGTH_LONG).show();
}

最佳答案

将你的代码放到线程中

new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
// do something
}
}).start();

并检查你的 AndroidManifest.xml 权限

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

关于php - 将输入数据从android发送到php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21623455/

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