gpt4 book ai didi

java - httppost 导致应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 11:04:52 27 4
gpt4 key购买 nike

我正在尝试通过 android studio 中的 java 将发布请求发送到我的 PHP 页面

首先我尝试了 httppost,但按下该键后应用程序崩溃了

然后我读到我必须使用新线程,我这样做了,但仍然

应用程序崩溃

代码如下:

 public void send () {
final String name,phone,fb;
EditText one,two,three;
one=(EditText) findViewById(R.id.editText);
two=(EditText) findViewById(R.id.editText2);
three=(EditText) findViewById(R.id.editText3);
name=one.getText().toString();
phone=two.getText().toString();
fb=three.getText().toString();
Thread T=new Thread(new Runnable() {
@Override
public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://test.com/test.php");
try {
List <NameValuePair> nameValuePairs= new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name",name));
nameValuePairs.add(new BasicNameValuePair("number",phone));
nameValuePairs.add(new BasicNameValuePair("fbname",fb));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response= httpclient.execute(httppost);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
});
T.start();
}

日志:http://pastebin.com/KQ9Y3rrG

最佳答案

确保您拥有在 list 文件中设置的与互联网通信的权限。否则,它将抛出 SecurityException 并破坏您的应用。

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

除此之外,我在您的日志中看不到任何可能导致异常的内容。

更新

检查用户提供的代码后,问题出在 onClick 事件本应触发 send() 方法,但在不知不觉中定义了它所需的 View v 参数。

解决方案(对于这个问题,这并不意味着它会让 httppost 工作)很简单,就是将参数添加到方法的定义中:

public void send (View v) {

关于java - httppost 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29859439/

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