java - httppost 导致应用程序崩溃

标签 java android android-studio

我正在尝试通过 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/

相关文章:

java - 将持久化实体保留在事务之外

java - Appium 检查器在 iOS 中找不到日期选择器的 xpath

android - Room Auto Migration 显示编译时错误

android - 请求 Android 视频通话功能的 API

php - E/ Volley : [1376] BasicNetwork. 执行请求 : Unexpected response code 404 for http://192. 168.0.109/connectphp/GetRestaurantData.php?restype=cafe

Android Studio 和 protobuf 错误 :(7, 0) 找不到 id 为 'com.google.protobuf' 的插件

java - 注解 @Value 在 Spring Boot 中不能正常工作?

java - Android Vitals 和异常处理

android - 如何在 Activity 启动时收到通知

Android studio 不建议导入