How to send a JSON object over Request with Android?

前端 未结 8 1856
慢半拍i
慢半拍i 2020-11-22 03:32

I want to send the following JSON text

{\"Email\":\"aaa@tbbb.com\",\"Password\":\"123456\"}

to a web service and read the response. I kno

8条回答
  •  难免孤独
    2020-11-22 03:49

    public class getUserProfile extends AsyncTask {
        JSONArray array;
        @Override
        protected JSONArray doInBackground(Void... params) {
    
            try {
                commonurl cu = new commonurl();
                String u = cu.geturl("tempshowusermain.php");
                URL url =new URL(u);
              //  URL url = new URL("http://192.168.225.35/jabber/tempshowusermain.php");
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setRequestProperty("Content-Type", "application/json");
                httpURLConnection.setRequestProperty("Accept", "application/json");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
                httpURLConnection.setDoInput(true);
                httpURLConnection.connect();
    
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("lid",lid);
    
    
                DataOutputStream outputStream = new DataOutputStream(httpURLConnection.getOutputStream());
                outputStream.write(jsonObject.toString().getBytes("UTF-8"));
    
                int code = httpURLConnection.getResponseCode();
                if (code == 200) {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
    
                    StringBuffer stringBuffer = new StringBuffer();
                    String line;
    
                    while ((line = bufferedReader.readLine()) != null) {
                        stringBuffer.append(line);
                    }
                    object =  new JSONObject(stringBuffer.toString());
                 //   array = new JSONArray(stringBuffer.toString());
                    array = object.getJSONArray("response");
    
                }
    
            } catch (Exception e) {
    
                e.printStackTrace();
            }
            return array;
    
    
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
    
    
        }
    
        @Override
        protected void onPostExecute(JSONArray array) {
            super.onPostExecute(array);
            try {
                for (int x = 0; x < array.length(); x++) {
    
                    object = array.getJSONObject(x);
                    ComonUserView commUserView=new ComonUserView();//  commonclass.setId(Integer.parseInt(jsonObject2.getString("pid").toString()));
                    //pidArray.add(jsonObject2.getString("pid").toString());
    
                    commUserView.setLid(object.get("lid").toString());
                    commUserView.setUname(object.get("uname").toString());
                    commUserView.setAboutme(object.get("aboutme").toString());
                    commUserView.setHeight(object.get("height").toString());
                    commUserView.setAge(object.get("age").toString());
                    commUserView.setWeight(object.get("weight").toString());
                    commUserView.setBodytype(object.get("bodytype").toString());
                    commUserView.setRelationshipstatus(object.get("relationshipstatus").toString());
                    commUserView.setImagepath(object.get("imagepath").toString());
                    commUserView.setDistance(object.get("distance").toString());
                    commUserView.setLookingfor(object.get("lookingfor").toString());
                    commUserView.setStatus(object.get("status").toString());
    
                    cm.add(commUserView);
                }
                custuserprof = new customadapterformainprofile(getActivity(),cm,Tab3.this);
              gridusername.setAdapter(custuserprof);
                //  listusername.setAdapter(custuserprof);
                } catch (Exception e) {
    
                    e.printStackTrace();
            }
        }
    

提交回复
热议问题