我是做安卓的,今天写了个用POST方式向服务器提交数据,一直提交不了,代码也不报错,打印出来是这个java.lang.NumberFormatException:Invalid int: 8081
这个8081是我的服务器端口号,求大神解答,谢谢!!
post请求有多种,不贴代码不好看,这是数字格式转换异常
这是我的代码,帮我看一下,谢谢!!publicclassHttpUtils{publicstaticStringsubmitPostData(StringstrUrlPath,Map<String,String>params,Stringencode){byte[]data=getRequestData(params,encode).toString().getBytes();//获得请求体try{URLurl=newURL(strUrlPath);HttpURLConnectionhttpURLConnection=(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));OutputStreamoutputStream=httpURLConnection.getOutputStream();outputStream.write(data,0,data.length);outputStream.close();intresponse=httpURLConnection.getResponseCode();if(response==HttpURLConnection.HTTP_OK){InputStreaminputStream=httpURLConnection.getInputStream();returndealResponseResult(httpURLConnection.getInputStream(),encode);}}catch(IOExceptione){e.printStackTrace();return"err:"+e.getMessage().toString();}return"-1";}privatestaticStringBuffergetRequestData(Map<String,String>params,Stringencode){StringBufferstringBuffer=newStringBuffer();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(Exceptione){e.printStackTrace();}returnstringBuffer;}privatestaticStringdealResponseResult(InputStreaminputStream,Stringencode){StringresultData=null;ByteArrayOutputStreambyteArrayOutputStream=newByteArrayOutputStream();byte[]data=newbyte[1024];intlen=0;if(inputStream!=null){try{while((len=inputStream.read(data))!=-1){byteArrayOutputStream.write(data,0,len);}resultData=newString(byteArrayOutputStream.toByteArray(),encode);}catch(IOExceptione){e.printStackTrace();}}returnresultData;}}
publicclassMainActivityextendsAppCompatActivity{privateButtonpost_btn;privateTextViewresult;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);result=(TextView)findViewById(R.id.result);post_btn=(Button)findViewById(R.id.btn);post_btn.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){post_btn_OnClick(view);}});}privatevoidpost_btn_OnClick(Viewview){StringstrRecSmsMsg="收短信测试";RecSmsToPost(strRecSmsMsg);}privatevoidRecSmsToPost(StringstrRecSmsMsg){StringstrNowDateTime=getNowDateTime("yyyy-MM-dd|HH:mm:ss");Map<String,String>params=newHashMap<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");StringstrUrlPath="http://192.168.31.177:8081"+"DataTime="+strNowDateTime;StringstrResult=HttpUtils.submitPostData(strUrlPath,params,"utf-8");result.setText(strResult);}privateStringgetNowDateTime(StringstrFormat){if(strFormat==""){strFormat="yyyy-MM-ddHH:mm:ss";}Datenow=newDate();SimpleDateFormatdateFormat=newSimpleDateFormat(strFormat);returndateFormat.format(now);}}
StringstrUrlPath="http://192.168.31.177:8081"+"DataTime="+strNowDateTime;
&DateTime...
这个是打印出现在的时间URL=URI?queryString
URI与queryString之间要加?(问号),也就是URI+?+queryString才是完整的URL格式。
queryString的多个参数之间用&连接。
另外,你的上述代码封装是“不支持同时提交多个同名参数的”。
这个问题解决了但我现在点击按钮直接就停止运行URL由URI与“可选的queryString”联合组成。当有queryString时,URI与queryString之间要有问号(?)。而你的程序中显然是缺少了问号的。我这里面没有什么URL=URI啊能不能说详细点我是个安卓菜鸟,还有要怎么才能同时提交多个数据呢这是现在的代码但运行点击按钮的时候直接停止运行
publicclassHttpUtils{publicstaticStringsubmitPostData(StringstrUrlPath,Map<String,String>params,Stringencode){byte[]data=getRequestData(params,encode).toString().getBytes();//获得请求体try{URLurl=newURL(strUrlPath);HttpURLConnectionhttpURLConnection=(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));OutputStreamoutputStream=httpURLConnection.getOutputStream();outputStream.write(data);//outputStream.close();intresponse=httpURLConnection.getResponseCode();if(response==HttpURLConnection.HTTP_OK){InputStreaminptStream=httpURLConnection.getInputStream();returndealResponseResult(inptStream);}}catch(IOExceptione){e.printStackTrace();return"err:"+e.getMessage().toString();}return"-1";}privatestaticStringBuffergetRequestData(Map<String,String>params,Stringencode){StringBufferstringBuffer=newStringBuffer();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(Exceptione){e.printStackTrace();}returnstringBuffer;}privatestaticStringdealResponseResult(InputStreaminputStream){StringresultData=null;ByteArrayOutputStreambyteArrayOutputStream=newByteArrayOutputStream();byte[]data=newbyte[1024];intlen=0;try{while((len=inputStream.read(data))!=-1){byteArrayOutputStream.write(data,0,len);}}catch(IOExceptione){e.printStackTrace();}resultData=newString(byteArrayOutputStream.toByteArray());returnresultData;}}
publicclassMainActivityextendsAppCompatActivity{privateButtonpost_btn;privateTextViewresult;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);result=(TextView)findViewById(R.id.result);post_btn=(Button)findViewById(R.id.btn);post_btn.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){post_btn_OnClick(view);}});}privatevoidpost_btn_OnClick(Viewview){StringstrRecSmsMsg="收短信测试";RecSmsToPost(strRecSmsMsg);openToast("提交测试完成");}privatevoidRecSmsToPost(StringstrRecSmsMsg){StringstrNowDateTime=getNowDateTime("yyyy-MM-dd|HH:mm:ss");Map<String,String>params=newHashMap<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");StringstrUrlPath="http://192.168.31.177:8081"+"?DateTime="+strNowDateTime;StringstrResult=HttpUtils.submitPostData(strUrlPath,params,"utf-8");result.setText(strResult);}privateStringgetNowDateTime(StringstrFormat){if(strFormat==""){strFormat="yyyy-MM-ddHH:mm:ss";}Datenow=newDate();SimpleDateFormatdateFormat=newSimpleDateFormat(strFormat);returndateFormat.format(now);}//弹出消息privatevoidopenToast(StringstrMsg){Toast.makeText(this,strMsg,Toast.LENGTH_LONG).show();}}你int类型的产量是不是传成字符串了??感觉是这个的问题
StringstrUrlPath="http://192.168.31.177:8081"+"DataTime="+strNowDateTime;
这8081后不需要跟/?吗?你把URL打印出来,应该是规范
还是不行把异常贴出来
问题找到了是android4.0之后要在子线程里面访问网络但我不知道怎么用OkHttp,大神给个qq嘛帮我解决下问题嘛谢谢!!!太乱了,你要POST就POST,你的时间戳也别附加到url后面,当然如果你一定要这样,那也可以,我估计你的URL有问题,只是建议你,要么断点,要么2分法调试,你可以先把完整URL直接在浏览器里试一试看看,明显没有加问号版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。