我尝试开发一个 Android 应用程序。在这个应用程序中,我需要向 PHP 页面发送一个 POST 请求。
我的代码在 Java 端:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/get.php");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("mail", "asdasd"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
try{
HttpResponse httpresponse = httpclient.execute(httppost);
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Didn't Happen",10).show();
}
清单.xml
<?
xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.misman"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:permission="android.permission.INTERNET"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.misman.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我从很多网站上搜索过,代码不断进入 catch 块。我也使用了 HttpURLConnection 并且没有工作。你能帮我解决什么问题吗?