GET 和 POST 网络 请求
0 COMMENTS / 2017年08月11日
GET 网络请求


tisubmit(View v){

    String userName = .getText().toString();
    String userPass = .getText().toString();
    (TextUtils.(userName) || TextUtils.(userPass)) {
        Toast.(, , Toast.).show();
    } {

        Thread(){
            run() {
                {
                    URL url=URL();
                    HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
                    urlConnection.setConnectTimeout();
                    urlConnection.setReadTimeout();
                    urlConnection.setRequestMethod();
                    urlConnection.connect();
                    code=urlConnection.getResponseCode();
                    Log.(, +code);
                    (code==) {
                        InputStream inputStream=urlConnection.getInputStream();
                        BufferedReader bufferedReader=BufferedReader(InputStreamReader(inputStream));
                        String line;
                        StringBuffer buffer=StringBuffer();
                        ((line=bufferedReader.readLine())!=) {
                            buffer.append(line);

                        }
                        String str=buffer.toString();
                        Message message=Message();
                        message.=;
                        message.=str;
                        Handler.sendMessage(message);
                        //请求 网络 数据 回调  
                    }
                } (MalformedURLException e) {
                    e.printStackTrace();
                } (IOException e) {
                    e.printStackTrace();
                }
            };
        }.start();
    }
}
Handler =Handler(){
    handleMessage(Message msg) {
        Log.(, String.(msg.));
        String shuju = String.(msg.);
        String value1 = ,value2=;
        {
            JSONObject myJsonObject = JSONObject(shuju);
            value1 = myJsonObject.getString();
            (value1 == ){
                JSONObject mydatat= JSONObject(shuju).getJSONObject();

                SharedPreferences sp = getSharedPreferences(, Context.);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString(, shuju);
                editor.putString(,mydatat.getString());
                editor.putString(,mydatat.getString());
                editor.commit();

            }

        }
        (JSONException e)
        {
        }
        (value1 == ) {

            Intent intent =Intent(LoginActivity.,HomeActivity.);
            startActivity(intent);
        }{
            Toast.(getApplicationContext(), ,
                    Toast.).show();
        }
    }


};



POST 网络请求

loadData(){
       Thread(){
           run() {
               SharedPreferences sp = getActivity().getSharedPreferences(, Context.);
               String id = sp.getString(, );
               String token = sp.getString(, );
               String path = ;
               {
                   URL url = URL(path);
                   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                   connection.setConnectTimeout();
                   connection.setRequestMethod();
                   String data = +id++token;
                   connection.setRequestProperty(,);
                   connection.setRequestProperty(, data.length()+);

                   connection.setDoOutput();
                   OutputStream outputStream = connection.getOutputStream();
                   outputStream.write(data.getBytes());

                   responseCode = connection.getResponseCode();
                   (responseCode ==){
                       InputStream inputStream = connection.getInputStream();
                       BufferedReader bufferedReader=BufferedReader(InputStreamReader(inputStream));
                       String line;
                       StringBuffer buffer=StringBuffer();
                       ((line=bufferedReader.readLine())!=) {
                           buffer.append(line);

                       }
                       String str=buffer.toString();
                       Log.(, +str);
                       Message message=Message();
                       message.=;
                       message.=str;
                       Handler.sendMessage(message);
                        //请求 网络 数据 回调  
                   }{

                   }
               } (MalformedURLException e) {
                   e.printStackTrace();
               } (ProtocolException e) {
                   e.printStackTrace();
               } (IOException e) {
                   e.printStackTrace();
               }
           };
       }.start();
   }

   Handler =Handler(){
       handleMessage(Message msg) {

           String shuju = String.(msg.);
           String value1 = ,value2=;
           {
               JSONObject result = JSONObject(shuju);num = result.length();
               JSONArray nameList = result.getJSONArray();length = nameList.length();
               String aa = ;
               (i = ; i < length; i++){JSONObject oj = nameList.getJSONObject(i);
                   oj.getString();
            .add(Music(oj.getString()+,oj.getString(),oj.getString(),oj.getString()));
               }
               ++;
               = DataAdapter((HomeActivity) getActivity(),);
               .setAdapter();

           }
           (JSONException e)
           {
           }

       }


   };


发表评论
NAME:

首页 / 模版 / 服务 / 新闻 / 技术 / 我们 / SEO /

地址:上海市松江区九亭镇涞寅路505弄贝尚湾1号907 电话:021-64796648 邮箱:info@dbestech.com Copyright@2016-2017 dbestech.com All rights reserved 版权所有:上海穆尼企业管理有限公司 沪ICP备16023578号-3