以下是使用的代码。
import android.app.Activity;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.util.ArrayList;
public class ContactUs extends Activity implements View.OnClickListener {
Button ok;
EditText name;
EditText email;
EditText message;
String resultname, resultemail, resultmessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_us);
//For the submit button
ok = (Button) findViewById(R.id.btn_submit);
ok.setOnClickListener(this);
//For the value of forms in database
name = (EditText) findViewById(R.id.et_name);
email = (EditText) findViewById(R.id.et_mail);
message = (EditText) findViewById(R.id.et_message);
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.btn_submit){
class asynctask extends AsyncTask<Void,Void,String>{
private void postdata (){
HttpClient httpClient = new DefaultHttpClient();
////zong ip address 192.168.8.103
HttpPost httpPost = new HttpPost("http://192.168.10.33:80/insert 1.php");
try {
ArrayList <NameValuePair> pairs = new ArrayList<NameValuePair>();
resultname = name.getText().toString();
resultemail = email.getText().toString();
resultmessage = message.getText().toString();
pairs.add(new BasicNameValuePair("Name",resultname));
pairs.add(new BasicNameValuePair("Email",resultemail));
pairs.add(new BasicNameValuePair("Message",resultmessage));
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = httpClient.execute(httpPost);
}catch (Exception e){
e.printStackTrace();
Log.e("Error",e.toString());
}
}
@Override
protected String doInBackground(Void... params) {
postdata();
return null;
}
}
new asynctask().execute((Void) null);
Toast.makeText(getApplicationContext(), "Thanks for your Registeration", Toast.LENGTH_LONG).show();
}
}
}
以下是我得到的错误
06-15 17:21:14.880 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at org.apache.http.HttpHost.<init>(HttpHost.java:83)
06-15 17:21:14.880 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:521)
06-15 17:21:14.880 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:511)
06-15 17:21:14.880 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:489)
06-15 17:21:14.880 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at com.example.harismahmood.fhfountainhouse.ContactUs$1asynctask.postdata(ContactUs.java:67)
06-15 17:21:14.880 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at com.example.harismahmood.fhfountainhouse.ContactUs$1asynctask.doInBackground(ContactUs.java:77)
06-15 17:21:14.881 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at com.example.harismahmood.fhfountainhouse.ContactUs$1asynctask.doInBackground(ContactUs.java:49)
06-15 17:21:14.881 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-15 17:21:14.881 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-15 17:21:14.881 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-15 17:21:14.881 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-15 17:21:14.881 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-15 17:21:14.881 14599-14901/com.example.harismahmood.fhfountainhouse W/System.err: at java.lang.Thread.run(Thread.java:838)
06-15 17:21:14.896 14599-14901/com.example.harismahmood.fhfountainhouse E/Error: java.lang.IllegalArgumentException: Host name may not be null
答案 0 :(得分:0)
尝试URLEncoder.encode,因为在&#34;插入1.php&#34;之间有空格。它不允许使用空格,因此您必须对其进行编码
HttpPost httpPost = new HttpPost(URLEncoder.encode("http://192.168.10.33:80/insert 1.php"));