2012-10-02 70 views
0

我一直在收到这个错误,我似乎无法找到解决方案。Android上使用httpclient获取logcat错误

10-02 06:08:23.928: E/AndroidRuntime(7306): FATAL EXCEPTION: main 
10-02 06:08:23.928: E/AndroidRuntime(7306): android.os.NetworkOnMainThreadException 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at libcore.io.IoBridge.connect(IoBridge.java:112) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at java.net.Socket.connect(Socket.java:842) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at com.samplefillform.MainActivity$1.onClick(MainActivity.java:53) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at android.view.View.performClick(View.java:3511) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at android.view.View$PerformClick.run(View.java:14105) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at android.os.Handler.handleCallback(Handler.java:605) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at android.os.Handler.dispatchMessage(Handler.java:92) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at android.os.Looper.loop(Looper.java:137) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at java.lang.reflect.Method.invoke(Method.java:511) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
10-02 06:08:23.928: E/AndroidRuntime(7306):  at dalvik.system.NativeStart.main(Native Method) 

而且我甚至不知道这段代码工作,如果这个错误将被删除,我一直在寻找一种方法来3天做这个现在仍然没有运气。

这个应用程序做的是用户输入一个名称,当按钮名称发送到远程服务器的形式,现在这是我的本地主机使用xampp。我在这里的源代码:

MainActivity.java

package com.samplefillform; 

import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends Activity { 
    Button send; 
    EditText name; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     name = (EditText) findViewById(R.id.etFName); 
     send = (Button) findViewById(R.id.bSend); 

     final String sname = name.getText().toString(); 

     send.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       HttpClient postClient = new DefaultHttpClient(); 
       String postReq = "http://10.0.2.2/formcode.php"; 
       HttpPost request = new HttpPost(postReq); 
       List<NameValuePair> postParams = new ArrayList<NameValuePair>(); 
       postParams.add(new BasicNameValuePair("element_8_1", sname)); 
       UrlEncodedFormEntity postEntity = null; 
       try { 
        postEntity = new UrlEncodedFormEntity(postParams); 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } 

       request.setEntity(postEntity); 

       @SuppressWarnings("unused") 
       HttpResponse response = null; 
       try { 
        response = postClient.execute(request); 
       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
} 

的activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <EditText 
     android:id="@+id/etFName" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

    <Button 
     android:id="@+id/bSend" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/etLName" 
     android:text="Send"/> 

</RelativeLayout> 

和清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.samplefillform" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="15" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_main" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

终于PHP文件处理表单

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>App Test Form</title> 
<link rel="stylesheet" type="text/css" href="./data/form_6/css/view.css" media="all" /> 
<script type="text/javascript" src="js/view.js"></script> 
<script type="text/javascript" src="js/calendar.js"></script> 
</head> 
<body id="main_body" > 

    <img id="top" src="images/top.png" alt="" /> 
    <div id="form_container"> 

     <h1><a>App Test Form</a></h1> 
     <form id="form_6" class="digitalfuture" method="post" action="#main_body"> 
        <div class="form_description"> 
      <h2>App Test Form</h2> 
      <p></p> 
     </div>      
      <ul > 

        <li id="li_8" > 
     <label class="description">Name <span id="required_8" class="required">*</span></label> 
     <span> 
      <input id="element_8_1" name= "element_8_1" class="element text" maxlength="255" size="8" value="" /> 
      <label>First</label> 
     </span> 
     <span> 
      <input id="element_8_2" name= "element_8_2" class="element text" maxlength="255" size="14" value="" /> 
      <label>Last</label> 
     </span><p class="guidelines" id="guide_8"><small>Please tell us your name</small></p> 
     </li>  <li id="li_9" > 
     <label class="description">Phone Number <span id="required_9" class="required">*</span></label> 
     <span> 
      <input id="element_9_1" name="element_9_1" class="element text" size="3" maxlength="3" value="" type="text" /> - 
      <label for="element_9_1">(###)</label> 
     </span> 
     <span> 
      <input id="element_9_2" name="element_9_2" class="element text" size="3" maxlength="3" value="" type="text" /> - 
      <label for="element_9_2">###</label> 
     </span> 
     <span> 
      <input id="element_9_3" name="element_9_3" class="element text" size="4" maxlength="4" value="" type="text" /> 
      <label for="element_9_3">####</label> 
     </span> 
     <p class="guidelines" id="guide_9"><small>Please tell us your phone number</small></p> 
     </li>  <li id="li_10" > 
     <label class="description">Alternate Phone Number </label> 
     <span> 
      <input id="element_10_1" name="element_10_1" class="element text" size="3" maxlength="3" value="" type="text" /> - 
      <label for="element_10_1">(###)</label> 
     </span> 
     <span> 
      <input id="element_10_2" name="element_10_2" class="element text" size="3" maxlength="3" value="" type="text" /> - 
      <label for="element_10_2">###</label> 
     </span> 
     <span> 
      <input id="element_10_3" name="element_10_3" class="element text" size="4" maxlength="4" value="" type="text" /> 
      <label for="element_10_3">####</label> 
     </span> 
     <p class="guidelines" id="guide_10"><small>Please tell us an alternate phone number to contact you at</small></p> 
     </li>  <li id="li_11" > 
     <label class="description" for="element_11">Email <span id="required_11" class="required">*</span></label> 
     <div> 
      <input id="element_11" name="element_11" class="element text medium" type="text" maxlength="255" value="[email protected]" /> 
     </div> 
     </li> 

    </div> 
    <img id="bottom" src="images/bottom.png" alt="" /> 
    </body> 
</html> 

请帮我解决这个问题。

回答

2

您有android.os.NetworkOnMainThreadException,因为您正在接受来自主要用户界面线程的web服务。

为了避免这种情况,你必须使用异步任务或线程,

send.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

new Thread(new Runnable() 
    { 

     @Override 
     public void run() 
     { 

       HttpClient postClient = new DefaultHttpClient(); 
       String postReq = "http://10.0.2.2/formcode.php"; 
       HttpPost request = new HttpPost(postReq); 
       List<NameValuePair> postParams = new ArrayList<NameValuePair>(); 
       postParams.add(new BasicNameValuePair("element_8_1", sname)); 
       UrlEncodedFormEntity postEntity = null; 
       try { 
        postEntity = new UrlEncodedFormEntity(postParams); 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } 

       request.setEntity(postEntity); 

       @SuppressWarnings("unused") 
       HttpResponse response = null; 
       try { 
        response = postClient.execute(request); 
       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }).start(); 


     }); 
+0

以及错误现在消失了,但是当我查看formcode.php没有改变,输入字段仍然是空的 – philip

+0

你提供了在清单中的Internet权限? –

+0

是的,先生,我做了... – philip

1

它正是所说的...... 在Android 3及更高版本中,您无法在主/ UI线程上进行网络调用。 你需要为它打开新的线程。在您的ClickListener地说:

new Thread(new Runnable() 
     { 

      @Override 
      public void run() 
      { 
       // TODO Auto-generated method stub 

      } 
     }).start(); 

把所有的网络代码run()函数内。