我正在制作一个应用程序以在其中执行POST操作。如下所示
//private boolean x = false;
url = "http://domain.com/login";
InputStream inputStream = null;
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(url);
if (connDesc != null)
{
HttpConnection httpConn;
httpConn = (HttpConnection)connDesc.getConnection();
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
encPostData.append("username",username);
encPostData.append("password",password);
byte[] postData = encPostData.toString().getBytes("UTF-8");
httpConn.setRequestProperty("Content-Length", String.valueOf(postData.length));
httpConn.openOutputStream().write(encPostData.getBytes());
try
{
int Response = httpConn.getResponseCode();
System.out.println("Response Code = "+Response+httpConn.getResponseCode());
if (Response == 200)
{
System.out.println("Response Code = "+Response+httpConn.getResponseCode());
inputStream = httpConn.openInputStream();
byte[] responseData = new byte[10000];
int length = 0;
StringBuffer rawResponse = new StringBuffer(10000);
while (-1 != (length = inputStream.read(responseData)))
{
rawResponse.append(new String(responseData, 0, length));
}
replyMessage = rawResponse.toString();
key = replyMessage.substring(12, replyMessage.length()-2);
}
else if(Response == 500)
{
Dialog.alert("User Details Incorrect");
}
else
{
System.out.println(Response);
}
}
catch (IOException e)
{
Dialog.alert("Connection not established");
}
}
else
{
Dialog.alert("Connection not established");
}
return key;
我现在想知道将在哪种服务类型下工作。
如果没有gprs / 3g,但有wifi连接,我想使我的应用程序正常工作。
或者反过来也..
无论如何,首先要确定可用的网络,然后再选择要通信的网络。
感谢您在这方面的帮助。
谢谢,
最佳答案
花一些时间阅读TransportInfo javadoc