我正在尝试将一些字符串发送到一些php文件,并获取JSON变量。现在,我被困在将字符串发送到我的php文件中,我尝试了许多教程的许多组合,所有这些组合都压在了:
httpclient.execute(httppost);
我真的不知道为什么。PS我在manifest.xml文件中授予了INTERNET权限。这是我的代码,除了我之外,其他人都可以使用。
public void send()
{
String msg = "Some message text";
if(msg.length()>0) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8888/file.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "01"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);// THE APP CRASH HERE!!!
Toast.makeText(getBaseContext(),"Sent",Toast.LENGTH_SHORT).show();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(getBaseContext(),"All fields are required", Toast.LENGTH_SHORT).show();
}
}
目前,我只想通过发送过程(接下来将处理JSON问题)。
谢谢你们。
这是LogCat:
288-299/system_process W/ActivityManager﹕ Force finishing activity com.example.testphppost/.MainActivity
11-05 17:37:56.011 288-299/system_process W/WindowManager﹕ Failure taking screenshot for (328x546) to layer 21010
11-05 17:37:56.294 37-89/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property
11-05 17:37:56.542 288-302/system_process W/ActivityManager﹕ Activity pause timeout for ActivityRecord{40f0e4c8 u0 com.example.testphppost/.MainActivity}
11-05 17:37:56.762 37-184/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property
11-05 17:37:58.141 2147-2147/? I/Process﹕ Sending signal. PID: 2147 SIG: 9
11-05 17:37:58.161 288-288/system_process I/ActivityManager﹕ Process com.example.testphppost (pid 2147) has died.
11-05 17:37:59.061 288-288/system_process W/InputMethodManagerService﹕ Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40f27770 attribute=null, token = android.os.BinderProxy@40d32668
任何的想法?
您可能正在主线程上发出请求。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句