superce的博客

https://www.jianshu.com/users/7f0c33d3ae69/subscriptions

android连接SSM发送和接收数据

 //注册账户
            URL url=null;
            HttpURLConnection httpURLConnection=null;
            String result=null,re="";
            BufferedReader bufferedReader=null;
            int y=0,l=0;
            Intent intent1 = new 
                Intent("com.example.space.text2.action.RECEIVER_REGISTER");

            String email = intent.getStringExtra("email");
            String password = intent.getStringExtra("password");
            //连接网络进行注册
            try{
                url = new URL("http://192.168.137.1:8080/SSM5/adduser.json");
            }catch(MalformedURLException e)
            {
                intent1.putExtra("registerstate","网络连接失败");

            }
            try{
                httpURLConnection =(HttpURLConnection)url.openConnection();
                try{
                    httpURLConnection.setRequestMethod("POST");
                }catch(ProtocolException g)
                {
                    l=httpURLConnection.getResponseCode();
                    intent1.putExtra("registerstate","httpURLConnection.setRequestMethod(\"POST\")失败"+String.valueOf(l));
                }

                httpURLConnection.setReadTimeout(5000);
                httpURLConnection.setConnectTimeout(5000);
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setInstanceFollowRedirects(true);


                BufferedWriter  bufferedWriter=null;
                OutputStream outputStream = httpURLConnection.getOutputStream();
                try{
                    bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
                    //bufferedWriter.write(jsonout.toString());
                    bufferedWriter.write("email="+email+"&"+"password="+password);
                }catch(IOException o){
                    Log.i("mss","bufferedWriter.write IOException");
                }
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

                JSONObject jsonObject = null;
                try{
                    result = httpURLConnection.getResponseMessage();
                    bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                }catch(IOException h)
                {
                    intent1.putExtra("registerstate","httpURLConnection.getResponseMessage();失败");
                }
                String line;
                try{
                    while((line=bufferedReader.readLine())!=null)
                    {
                        re+=line;
                    }
                }catch(IOException e){
                    intent1.putExtra("registerstate","bufferedReader.readLine()失败");
                }
                Log.i("mss",re);

                try{

                    JSONObject jsonObject1 = new JSONObject(re);
                    if(jsonObject1.getInt("status")==1)
                    {
                        //注册成功
                        intent1.putExtra("registerstate","注册成功");
                    }
                    else{
                        intent1.putExtra("registerstate","注册失败");
                    }
                    Log.i("mss","jsonObject1.getInt(\"status\")"+String.valueOf(jsonObject1.getInt("status")));

                }catch(JSONException b)
                {
                    intent1.putExtra("registerstate","new JSONArray(re);失败");
               }

            }
            catch(IOException r)
            {
                intent1.putExtra("registerstate","HttpURLConnection IOException失败");

            }
            catch(NullPointerException r){
                intent1.putExtra("registerstate","HttpURLConnection NullPointerException!\n请连接网络后再注册");
            }



            //返回结果
                //已经注册过
                //注册成功
                //注册失败,请换一个


            //intent1.putExtra("registerstate","注册成功"+result+"      "+re+"<"+String.valueOf(y)+" "+String.valueOf(l));
            sendBroadcast(intent1);
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/superce/article/details/79962611
个人分类: Android
上一篇Android studio解析来自SSM的json的一个小问题
下一篇SSM处理接收到的android传来的request请求
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭