Android HttpPost。将数据发送到PHP服务器上的表单
问题描述:
我使用最新的Java Eclipse软件,当我运行此HttpPost代码时,模拟器崩溃。我在我的笔记本电脑上安装了uniserver,因此我将它用作服务器。Android HttpPost。将数据发送到PHP服务器上的表单
此代码应该从以前的类调用编辑文本数据,并且使用HttpPost请求这个数据上传到在线表单上各自的领域。
编辑的文本数据是3个字段:“从”,“To”和“消息”。而且我在服务器上创建的表单也有这些相同的字段来输入数据。 (“http://19x.xx.xx.xxx/androidp2p/testform.php”)其中,19x.xx.xx.xxx是我的(本地主机)IP地址。
我正确地从以前的类拉动的数据和我的代码类似于HttpPost的例子,我在网上找到,但我不知道为什么会崩溃。
我附HttpPost方法,我想看看我是否能得到任何援助。提前感谢你。
方法1:
String myBreadu, myBreadr, myBreadm;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle myBasket = getIntent().getExtras();
myBreadu = myBasket.getString("keyfrom");
myBreadr = myBasket.getString("keyto");
myBreadm = myBasket.getString("keymsg");
// Create a new HttpClient and Post Header
HttpClient client = new DefaultHttpClient();
String postURL = ("http://19x.xx.xx.xxx/androidp2p/testform.php");
HttpPost post = new HttpPost(postURL);
try {
// Add the data
List<NameValuePair> pairs = new ArrayList<NameValuePair>(3);
pairs.add(new BasicNameValuePair("keysendu", myBreadu));
pairs.add(new BasicNameValuePair("keysendr", myBreadr));
pairs.add(new BasicNameValuePair("keysendm", myBreadm));
UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(pairs);
post.setEntity(uefe);
// Execute the HTTP Post Request
HttpResponse response = client.execute(post);
// Convert the response into a String
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE", EntityUtils.toString(resEntity));
}
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
} catch (ClientProtocolException cpe) {
cpe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
方法2:
String myBreadu, myBreadr, myBreadm;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle myBasket = getIntent().getExtras();
myBreadu = myBasket.getString("keyfrom");
myBreadr = myBasket.getString("keyto");
myBreadm = myBasket.getString("keymsg");
String result = null;
// Create a new HttpClient and Post Header
HttpClient client = new DefaultHttpClient();
String postURL = ("http://186.45.107.129/androidp2p/testform.php");
HttpPost post = new HttpPost(postURL);
try {
// Add the data
List<NameValuePair> pairs = new ArrayList<NameValuePair>(3);
pairs.add(new BasicNameValuePair("keysendu", myBreadu));
pairs.add(new BasicNameValuePair("keysendr", myBreadr));
pairs.add(new BasicNameValuePair("keysendm", myBreadm));
UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(pairs);
post.setEntity(uefe);
// Execute the HTTP Post Request
HttpResponse response = client.execute(post);
// Convert the response into a String
HttpEntity resEntity = response.getEntity();
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String l = "";
StringBuilder sb = new StringBuilder();
while ((l = rd.readLine()) != null) {
sb.append(l + "\n");
}
rd.close();
String result = sb.toString(); // this line gives an error "Duplicate local variable result"
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
} catch (ClientProtocolException cpe) {
cpe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
这是testform.PHP
测试表单
来源:为:
消息:
我可以补充一点好吗?我不知道我是否应该被直接发送数据到窗体或这个其他PHP页面我有..
通过我在日志中得到错误,当我尝试HttpPost的方式是:
FATAL EXCEPTION: main
03-07 11:36:23.226: E/AndroidRuntime(1490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.keegan/com.project.keegan.SendPostMethod}: android.os.NetworkOnMainThreadException
03-07 11:36:23.226: E/AndroidRuntime(1490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-07 11:36:23.226: E/AndroidRuntime(1490): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-07 11:36:23.226: E/AndroidRuntime(1490): at android.app.ActivityThread.access$600(ActivityThread.java:130)
对不起,如果这是太多的信息家伙。谢谢。
答
您需要使用AsyncTask才能完成所有网络操作。
您的网络操作会占用大量的时间,如果它是在主UI线程上完成的UI会得到响应。如果您的用户界面长时间冻结,该应用可能会被操作系统杀死。
因此的Android 4+使得强制使用后台线程执行网络操作。
把代码做的网络活动中doInBacground()
和使用所有的AsyncTask。
这是你的AsyncTask会是什么样子:
private class SendData extends AsyncTask<String, Integer, Void> {
protected void doInBackground() {
// Create a new HttpClient and Post Header
HttpClient client = new DefaultHttpClient();
String postURL = ("http://19x.xx.xx.xxx/androidp2p/testform.php");
HttpPost post = new HttpPost(postURL);
try {
// Add the data
List<NameValuePair> pairs = new ArrayList<NameValuePair>(3);
pairs.add(new BasicNameValuePair("keysendu", myBreadu));
pairs.add(new BasicNameValuePair("keysendr", myBreadr));
pairs.add(new BasicNameValuePair("keysendm", myBreadm));
UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(pairs);
post.setEntity(uefe);
// Execute the HTTP Post Request
HttpResponse response = client.execute(post);
// Convert the response into a String
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE", EntityUtils.toString(resEntity));
}
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
} catch (ClientProtocolException cpe) {
cpe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
protected void onProgressUpdate() {
//called when the background task makes any progress
}
protected void onPreExecute() {
//called before doInBackground() is started
}
protected void onPostExecute() {
//called after doInBackground() has finished
}
}
,并且可以使用new SendData().execute("");
不,信息是永远不嫌多的任何地方调用它! :) – Swayam 2013-03-07 16:06:02