我是做安卓的,今天写了个用POST方式向服务器提交数据,一直提交不了,代码也不报错,打印出来是这个java.lang.NumberFormatException:Invalid int: 8081
这个8081是我的服务器端口号,求大神解答,谢谢!!
8081是不是字符串,要转换成int
这是我的代码 要怎么转换啊
public class HttpUtils {
public static String submitPostData(String strUrlPath, Map<String,String> params,String encode){
byte[] data = getRequestData(params,encode).toString().getBytes();//获得请求体
try {
URL url = new URL(strUrlPath);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(3000);//设置连接超时时间
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length",String.valueOf(data.length));
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(data,0,data.length);
outputStream.close();
int response = httpURLConnection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK){
InputStream inputStream = httpURLConnection.getInputStream();
return dealResponseResult(httpURLConnection.getInputStream(),encode);
}
} catch (IOException e) {
e.printStackTrace();
return "err: "+e.getMessage().toString();
}
return "-1";
}
private static StringBuffer getRequestData(Map<String, String> params, String encode) {
StringBuffer stringBuffer = new StringBuffer();
try {
for (Map.Entry<String,String> entry : params.entrySet()){
stringBuffer.append(entry.getKey())
.append("=")
.append(URLEncoder.encode(entry.getValue(),encode))
.append("&");
}
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
}catch (Exception e){
e.printStackTrace();
}
return stringBuffer;
}
private static String dealResponseResult(InputStream inputStream,String encode) {
String resultData = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
if (inputStream != null) {
try {
while ((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
resultData = new String(byteArrayOutputStream.toByteArray(),encode);
} catch (IOException e) {
e.printStackTrace();
}
}
return resultData;
}
}
public class MainActivity extends AppCompatActivity{
private Button post_btn;
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
post_btn = (Button) findViewById(R.id.btn);
post_btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
post_btn_OnClick(view);
}
});
}
private void post_btn_OnClick(View view){
String strRecSmsMsg = "收短信测试";
RecSmsToPost(strRecSmsMsg);
}
private void RecSmsToPost(String strRecSmsMsg){
String strNowDateTime = getNowDateTime("yyyy-MM-dd|HH:mm:ss");
Map<String,String> params = new HashMap<String, String>();
params.put("shopId","371");
// params.put("advertAreaId","1");
// params.put("mode","iisue");
// params.put("call","shopService.qryAdvertList");
// params.put("params","{\"advertAreaId\":\"1\",\"shopId\":\"371\"}\n");
// params.put("version","1.0.0");
String strUrlPath = "http://192.168.31.177:8081" + "DataTime=" + strNowDateTime;
String strResult = HttpUtils.submitPostData(strUrlPath,params,"utf-8");
result.setText(strResult);
}
private String getNowDateTime(String strFormat){
if (strFormat == ""){
strFormat = "yyyy-MM-dd HH:mm:ss";
}
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
return dateFormat.format(now);
}
}