有效的HTTP-Post在android中不起作用

时间:2014-01-09 11:48:51

标签: android http

我有以下代码通过HTTP Post发送数据。当我通过RESTCLIENT(Chrome扩展程序)发送http帖子时,数据被正确传输并存储在MySQL数据库中。但是当我通过我的Android服务发送数据时,没有任何东西到来。

private HttpClient mHttpClient;
private HttpPost mHttpPost = new HttpPost("http:/.../main.php");

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("mId", "\"" + getId() + "\""));
            nameValuePairs.add(new BasicNameValuePair("server", "\"localhost\""));
            nameValuePairs.add(new BasicNameValuePair("name", "\"xxx\""));
            nameValuePairs.add(new BasicNameValuePair("pass", "\"xxx\""));
            nameValuePairs.add(new BasicNameValuePair("debug", "\"true\""));
            nameValuePairs.add(new BasicNameValuePair("start", "\"" + formatterStartTime.format(startTime) + "\""));
            mHttpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            Log.i("HRV", "insert measure: " + nameValuePairs.toString());

            HttpResponse response = mHttpClient.execute(mHttpPost);
            response.getEntity().consumeContent();

发送http post后编辑:http://pastebin.com/FgA4TLgB日志输出

Edit2:我正在使用此方法检查互联网连接

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

我在manifest.xml中也有以下代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.hsn.hrv.app"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="de.hsn.hrv.app.MainActivity"
        android:label="@string/app_name" 
        android:launchMode="singleTop" 
        android:configChanges="orientation|screenSize" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="de.hsn.hrv.app.PreferenceActivity"
        android:label="@string/title_activity_preference" >
    </activity>
    <service android:name="de.hsn.hrv.service.BluetoothHandlerService"></service>
</application>

</manifest>

发送http帖子的代码在于服务。

1 个答案:

答案 0 :(得分:0)

我使用Volley进行REST API调用,在您需要使用StringRequest的情况下,只需很少的实现就可以完美地工作。我使用过apach httpclient,你的代码是正确的,所以问题可能不存在。