您好我正在尝试使用 httpclient 将数据发送到服务器。
这是我的代码
public class MainActivity extends Activity implements OnClickListener {
private String TAG;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button press = (Button) findViewById(R.id.button1);
press.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
String usrName = ((EditText) findViewById(R.id.usrName)).getText()
.toString().trim();
String age = ((EditText) findViewById(R.id.age)).getText()
.toString().trim();
Toast.makeText(getApplicationContext(),
"username:: " + usrName + "---Age::" + age,
Toast.LENGTH_SHORT).show();
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.40");
String resp = null;
try {
// create data to POST
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
5);
nameValuePairs.add(new BasicNameValuePair("UserName", usrName));
nameValuePairs.add(new BasicNameValuePair("Age", age));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
resp = response.toString();
} catch (ClientProtocolException e) {
System.out.println("---" + e.getMessage());
} catch (IOException e1) {
System.out.println("---" + e1.getMessage());
}
Log.i(TAG, "RTT inside post-data: " + time_passed);
Log.i(TAG, resp.toString());
break;
case R.id.send:
default:
break;
}
}
}
我在 logcat 中收到此错误
06-07 18:23:45.650: E/AndroidRuntime(2387): FATAL EXCEPTION: main
06-07 18:23:45.650: E/AndroidRuntime(2387): android.os.NetworkOnMainThreadException
06-07 18:23:45.650: E/AndroidRuntime(2387): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-07 18:23:45.650: E/AndroidRuntime(2387): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-07 18:23:45.650: E/AndroidRuntime(2387): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-07 18:23:45.650: E/AndroidRuntime(2387): at libcore.io.IoBridge.connect(IoBridge.java:112)
06-07 18:23:45.650: E/AndroidRuntime(2387): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-07 18:23:45.650: E/AndroidRuntime(2387): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-07 18:23:45.650: E/AndroidRuntime(2387): at java.net.Socket.connect(Socket.java:842)
06-07 18:23:45.650: E/AndroidRuntime(2387): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-07 18:23:45.650: E/AndroidRuntime(2387): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-07 18:23:45.650: E/AndroidRuntime(2387): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-07 18:23:45.650: E/AndroidRuntime(2387): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-07 18:23:45.650: E/AndroidRuntime(2387): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-07 18:23:45.650: E/AndroidRuntime(2387): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-07 18:23:45.650: E/AndroidRuntime(2387): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-07 18:23:45.650: E/AndroidRuntime(2387): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-07 18:23:45.650: E/AndroidRuntime(2387): at com.sakeesoft.android.MainActivity.onClick(MainActivity.java:104)
06-07 18:23:45.650: E/AndroidRuntime(2387): at android.view.View.performClick(View.java:3511)
06-07 18:23:45.650: E/AndroidRuntime(2387): at android.view.View$PerformClick.run(View.java:14105)
06-07 18:23:45.650: E/AndroidRuntime(2387): at android.os.Handler.handleCallback(Handler.java:605)
06-07 18:23:45.650: E/AndroidRuntime(2387): at android.os.Handler.dispatchMessage(Handler.java:92)
06-07 18:23:45.650: E/AndroidRuntime(2387): at android.os.Looper.loop(Looper.java:137)
06-07 18:23:45.650: E/AndroidRuntime(2387): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-07 18:23:45.650: E/AndroidRuntime(2387): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 18:23:45.650: E/AndroidRuntime(2387): at java.lang.reflect.Method.invoke(Method.java:511)
06-07 18:23:45.650: E/AndroidRuntime(2387): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-07 18:23:45.650: E/AndroidRuntime(2387): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-07 18:23:45.650: E/AndroidRuntime(2387): at dalvik.system.NativeStart.main(Native Method)
请帮助我了解 HTTPClient 的磨损情况。谢谢..