我创建了一个将数据发送到网络的android程序。
这是我的代码:
public void sendToweb() {
// get the message from the message text box
String msg = systemId1.getText().toString();
// make sure the fields are not empty
if (msg.length() > 0) {
HttpResponse response = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://eservices.rondssolar.com/xbusiness.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2);
nameValuePairs.add(new BasicNameValuePair("submit", "Save Tracker Data"));
nameValuePairs.add(new BasicNameValuePair("systemId", msg));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
Toast.makeText(getApplicationContext(),
"DATA SEND" + response.getStatusLine().toString(),
Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
}
} else {
// display message if text fields are empty
Toast.makeText(getBaseContext(), "All field are required",
Toast.LENGTH_SHORT).show();
}
}
我的问题是我没有收到任何回复。我发送的数据也不在网上。我认为我的回复= httpclient.execute(httppost);代码无法正常工作
答案 0 :(得分:0)
您可以使用Json格式发布参数:
Thread tSendToweb = new Thread(new Runnable()
{
@Override
public void run()
{
HttpClient hc = new DefaultHttpClient();
String message;
HttpPost p = new HttpPost("http://eservices.rondssolar.com/xbusiness.php");
JSONObject object = new JSONObject();
try
{
object.put("systemId", systemId);
}
catch (Exception ex)
{
ex.printStackTrace();
}
message = object.toString();
try
{
p.setEntity(new StringEntity(message, "UTF8"));
p.setHeader("Content-type", "application/json");
hc.execute(p);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
tAddLocation.start();