首先:
您需要使用以下方法在异步线程中执行所有网络任务:
public class PostData extends AsyncTask<String, Void, String>{
{
@Override
protected String doInBackground(String... params) {
//put all your network code here
}
第二:
创建你的 http 请求:我在这里假设电子邮件、用户名和 IMG 作为变量。
String server ="http://myurl.com/user.php? email=[" + EMAIL + "]&username=[" + USERNAME + "]&password[" + PASS + "]&img_url=["+IMG + "]";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(server);
//httppost.setHeader("Accept", "application/json");
httppost.setHeader("Accept", "application/x-www-form-urlencoded");
//httppost.setHeader("Content-type", "application/json");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
third:
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("JSONdata", Object));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
try {
HttpResponse response =httpclient.execute(httppost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
现在简单地查询您的响应处理程序,即在这种情况下的响应。
不要忘记在您的 androidManifest.xml 中添加 INTERNET 权限
希望这可以帮助!